Categories
TypeScript Answers

How to fix the “Property ‘includes’ does not exist on type ‘string[]'” error with TypeScript?

Sometimes, we want to fix the "Property ‘includes’ does not exist on type ‘string[]’" error with TypeScript.

In this article, we’ll look at how to fix the "Property ‘includes’ does not exist on type ‘string[]’" error with TypeScript.

How to fix the "Property ‘includes’ does not exist on type ‘string[]’" error with TypeScript?

To fix the "Property ‘includes’ does not exist on type ‘string[]’" error with TypeScript, we can add the 'es2017' option to the compilerOptions.lib option in tsconfig.json.

For instance, we write

{
  //...
  "compilerOptions": {
    //...
    "lib": ["es6", "dom", "es2017"],
    //...
    "target": "es5"
    //...
  }
  //...
}

to add "es2017" into the lib array so that the TypeScript compiler knows that ES2017 features are available for use in our TypeScript project.

Conclusion

To fix the "Property ‘includes’ does not exist on type ‘string[]’" error with TypeScript, we can add the 'es2017' option to the compilerOptions.lib option in tsconfig.json.

Categories
TypeScript Answers

How to use typed arrays with TypeScript?

Sometimes, we want to use typed arrays with TypeScript.

In this article, we’ll look at how to use typed arrays with TypeScript.

How to use typed arrays with TypeScript?

To use typed arrays with TypeScript, we can use the Array type.

For instance, we write

const possessions: Array<Thing> = new Array<Thing>();

to create the possessions variable of type Array<Thing>, which means all entries in the possessions array must be of type Thing.

We can also shorten it to

const possessions: Thing[] = [];

Conclusion

To use typed arrays with TypeScript, we can use the Array type.

Categories
TypeScript Answers

How to use enum as index key type in TypeScript?

Sometimes, we want to use enum as index key type in TypeScript.

In this article, we’ll look at how to use enum as index key type in TypeScript.

How to use enum as index key type in TypeScript?

To use enum as index key type in TypeScript, we can use the in operator.

For instance, we write

enum DialogType {
  Options,
  Help,
}

const openDialogs: { [key in DialogType]?: Dialog } = {
  [DialogType.Options]: undefined,
};

to create the DialogType enum.

Then we use it in a type by setting openDialogs to the { [key in DialogType]?: Dialog } type.

We set the index signature’s type to key in DialogType to make sure the keys of openDialogs must be any one of the DialogType enum values.

Therefore, using [DialogType.Options] as a key value wouldn’t make the TypeScript compiler raise an error.

Conclusion

To use enum as index key type in TypeScript, we can use the in operator.

Categories
TypeScript Answers

How to declare a function that throws an error in TypeScript?

Sometimes, we want to declare a function that throws an error in TypeScript.

In this article, we’ll look at how to declare a function that throws an error in TypeScript.

How to declare a function that throws an error in TypeScript?

To declare a function that throws an error in TypeScript, we can use never as the return type.

For instance, we write

const test = (): never => {
  throw new Error();
};

to create the test function that throws an error.

We set its return type to the never type so the TypeScript compiler will know that the function always throws an error.

Conclusion

To declare a function that throws an error in TypeScript, we can use never as the return type.

Categories
TypeScript Answers

How to merge two interfaces with TypeScript?

Sometimes, we want to merge two interfaces with TypeScript.

In this article, we’ll look at how to merge two interfaces with TypeScript.

How to merge two interfaces with TypeScript?

To merge two interfaces with TypeScript, we can use extends to extend multiple interfaces.

For instance, we write

interface IFooBar extends IFoo, IBar {}

to create the IFooBar that extends IFoo and IBar.

This means IFooBar has all the members from both interfaces inside.

Conclusion

To merge two interfaces with TypeScript, we can use extends to extend multiple interfaces.