Categories
TypeScript Answers

How to generate TypeScript declaration files from JavaScript code?

Sometimes, we want to generate TypeScript declaration files from JavaScript code.

In this article, we’ll look at how to generate TypeScript declaration files from JavaScript code.

How to generate TypeScript declaration files from JavaScript code?

To generate TypeScript declaration files from JavaScript code, we can use the dts-gen module.

To install it, we run

npm install -g dts-gen

Then we install the module that we want to generate types from.

For instance, if we want to generate types for yargs, we run

npm install -g yargs

Then we run

dts-gen -m yargs

to generate the type file for yargs.

We should get the yargs.d.ts file in our project folder.

Conclusion

To generate TypeScript declaration files from JavaScript code, we can use the dts-gen module.

Categories
TypeScript Answers

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

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

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

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

To fix the "Property ‘replaceAll’ does not exist on type ‘string’" error with TypeScript, we should add "ES2021.String" into the compilerOptions.lib array.

To do this, we write

{
  //..,
  "compilerOptions": {
    //...,
    "lib": [
      //...,
      "ES2021.String"
    ]
  }
}

to add "ES2021.String" into compilerOptions.lib in tsconfig.json.

Then the TypeScript compiler will know that the string replaceAll method is available for use in our TypeScript project.

Conclusion

To fix the "Property ‘replaceAll’ does not exist on type ‘string’" error with TypeScript, we should add "ES2021.String" into the compilerOptions.lib array.

Categories
TypeScript Answers

How to fix the “Exceeds maximum line length of 120” TypeScript TSLint error?

Sometimes, we want to fix the "Exceeds maximum line length of 120" TypeScript TSLint error.

In this article, we’ll look at how to fix the "Exceeds maximum line length of 120" TypeScript TSLint error.

How to fix the "Exceeds maximum line length of 120" TypeScript TSLint error?

To fix the "Exceeds maximum line length of 120" TypeScript TSLint error, we can add the // tslint:disable-next-line:max-line-length comment before the line that causes the error.

For instance, we write

// tslint:disable-next-line:max-line-length
import { PersonsSingleAccountComponent } from "../persons-information/fragments/persons-single-account/persons-single-account-bookings/persons-single-account-bookings.component";

to add // tslint:disable-next-line:max-line-length before the line that’s longer than 120 character to disable the TSLint max-line-length check on the line.

Conclusion

To fix the "Exceeds maximum line length of 120" TypeScript TSLint error, we can add the // tslint:disable-next-line:max-line-length comment before the line that causes the error.

Categories
TypeScript Answers

How to disable TSLint in VS Code?

Sometimes, we want to disable TSLint in VS Code.

In this article, we’ll look at how to disable TSLint in VS Code.

How to disable TSLint in VS Code?

To disable TSLint in VS Code, we can set the "typescript.validate.enable" setting to false in our VS Code settings.json file.

To do this, we write

{
  //...
  "typescript.validate.enable": false
  //...
}

to set "typescript.validate.enable" to false in settings.json to disable TSLint.

Conclusion

To disable TSLint in VS Code, we can set the "typescript.validate.enable" setting to false in our VS Code settings.json file.

Categories
TypeScript Answers

How to enumerate properties on an object with TypeScript?

Sometimes, we want to enumerate properties on an object with TypeScript.

In this article, we’ll look at how to enumerate properties on an object with TypeScript.

How to enumerate properties on an object with TypeScript?

To enumerate properties on an object with TypeScript, we can use the for-of loop and the Object.entries method.

For instance, we write

class StationGuide {
  station1: any;
  station2: any;
  station3: any;

  constructor() {
    this.station1 = null;
    this.station2 = null;
    this.station3 = null;
  }
}

const a = new StationGuide();

for (const [key, val] of Object.entries(a)) {
  console.log(key, val);
}

to create a StationGuide class instance and assign it to a.

Then we convert the object to an array of key-value pair arrays with Object.entries.

Then we loop through the array of key-value pair arrays with the for-of loop.

We destructure the key and val key and value from each array and log their values.

Conclusion

To enumerate properties on an object with TypeScript, we can use the for-of loop and the Object.entries method.