Categories
TypeScript Answers

How to use TypeScript with native ES6 Promises?

Sometimes, we want to use TypeScript with native ES6 Promises.

In this article, we’ll look at how to use TypeScript with native ES6 Promises.

How to use TypeScript with native ES6 Promises?

To use TypeScript with native ES6 Promises, we can use the Promise constructor.

For instance, we write

const p = new Promise<string>((resolve, reject) => {
  resolve("a string");
});

to create a new Promise instance.

We specify that the promise resolves to a string value with <string>.

And we call resolve with a string to make the promise resolve to a string value.

Conclusion

To use TypeScript with native ES6 Promises, we can use the Promise constructor.

Categories
TypeScript Answers

How to check for undefined in Typescript?

Sometimes, we want to check for undefined in Typescript.

In this article, we’ll look at how to check for undefined in Typescript.

How to check for undefined in Typescript?

To check for undefined in Typescript, we can use the === operator.

For instance, we write

if (email === undefined) {
}

to check if the email variable is undefined.

Conclusion

To check for undefined in Typescript, we can use the === operator.

Categories
TypeScript Answers

How to fix the “An async function or method in ES5/ES3 requires the ‘Promise’ constructor” with TypeScript?

Sometimes, we want to fix the "An async function or method in ES5/ES3 requires the ‘Promise’ constructor" with TypeScript.

In this article, we’ll look at how to fix the "An async function or method in ES5/ES3 requires the ‘Promise’ constructor" with TypeScript.

How to fix the "An async function or method in ES5/ES3 requires the ‘Promise’ constructor" with TypeScript?

To fix the "An async function or method in ES5/ES3 requires the ‘Promise’ constructor" with TypeScript, we can add the 'es2015' into the compilerOptions.lib array.

For instance, we write

{
  //...
  "compilerOptions": {
    "lib": ["es2015"]
  }
  //...
}

to set compilerOptions.lib to ["es2015"] so that the TypeScript compiler can assume that the Promise constructor is available globally in our project.

Conclusion

To fix the "An async function or method in ES5/ES3 requires the ‘Promise’ constructor" with TypeScript, we can add the 'es2015' into the compilerOptions.lib array.

Categories
TypeScript Answers

How to fix ‘console.log are not allowed’ error with TypeScript tslint?

Sometimes, we want to fix ‘console.log are not allowed’ error with TypeScript tslint.

In this article, we’ll look at how to fix ‘console.log are not allowed’ error with TypeScript tslint.

How to fix ‘console.log are not allowed’ error with TypeScript tslint?

To fix ‘console.log are not allowed’ error with TypeScript tslint, we can disable the no-console rule.

To do this, we write

{
  //...
  "rules": {
    "no-console": false
  }
  //...
}

in tslint.json in our project.

Conclusion

To fix ‘console.log are not allowed’ error with TypeScript tslint, we can disable the no-console rule.

Categories
TypeScript Answers

How to use the spread syntax and new Set() together with TypeScript?

Sometimes, we want to use the spread syntax and new Set() together with TypeScript.

In this article, we’ll look at how to use the spread syntax and new Set() together with TypeScript.

How to use the spread syntax and new Set() together with TypeScript?

To use the spread syntax and new Set() together with TypeScript, we can set the downlevelIteration option to true is tsconfig.json.

For instance, we write

{
  //...
  "compilerOptions": {
    "downlevelIteration": true
  }
  //...
}

to set the downlevelIteration option to true in tsconfig.json.

Then we can use the spread syntax with sets by writing

const uniques = [...new Set([1, 2, 3, 1, 1])];

as we do with JavaScript without the TypeScript compiler throwing errors.

Conclusion

To use the spread syntax and new Set() together with TypeScript, we can set the downlevelIteration option to true is tsconfig.json.