Categories
TypeScript Answers

How to produce a .d.ts type definition file from an existing JavaScript library?

Sometimes, we want to produce a .d.ts type definition file from an existing JavaScript library.

In this article, we’ll look at how to produce a .d.ts type definition file from an existing JavaScript library.

How to produce a .d.ts type definition file from an existing JavaScript library?

To produce a .d.ts type definition file from an existing JavaScript library, we can use the dts-gen package or create it manually.

For instance, we run

npm install -g dts-gen

to install the dts-gen package globally.

Then we run

dts-gen -m <your-module>

to create the TypeScript type definition files for <your-module>.

We can also create a .d.ts file manually and add

declare module "foo";

where 'foo' is the module we’re trying to import.

This will let us import foo with the any type.

Conclusion

To produce a .d.ts type definition file from an existing JavaScript library, we can use the dts-gen package or create it manually.

Categories
TypeScript Answers

How to define TypeScript callback types?

Sometimes, we want to define TypeScript callback types.

In this article, we’ll look at how to define TypeScript callback types.

How to define TypeScript callback types?

To define TypeScript callback types, we can set the callback to have a function with the signature and return type.

For instance, we write

class C {
  public myCallback: () => void;

  public doWork(): void {
    this.myCallback();
  }
}

to set the myCallback method’s type to () => void.

() means myCallback has an empty signature.

And void myCallback returns nothing.

Conclusion

To define TypeScript callback types, we can set the callback to have a function with the signature and return type.

Categories
TypeScript Answers

How to add public static const variables in TypeScript?

Sometimes, we want to add public static const variables in TypeScript

In this article, we’ll look at how to add public static const variables in TypeScript.

How to add public static const variables in TypeScript?

To add public static const variables in TypeScript, we can add static getters.

For instance, we write

export class C {
  public static get FOO(): string {
    return "foo";
  }
}

to create the static getter FOO which returns 'foo'.

We make it static with static so we don’t need to instantiate the class to access it.

So we can use it by writing

C.FOO

Conclusion

To add public static const variables in TypeScript, we can add static getters.

Categories
TypeScript Answers

How to get the union of the keys of a TypeScript interface?

Sometimes, we want to get the union of the keys of a TypeScript interface.

In this article, we’ll look at how to get the union of the keys of a TypeScript interface.

How to get the keys of a TypeScript interface?

To get the union of the keys of a TypeScript interface, we can use the keyof keyword.

For instance, we write

interface Person {
  name: string;
  age: number;
  location: string;
}

type Keys = keyof Person;

to create the Keys type by setting it to keyof Person.

keyof Person returns the literal type "name" | "age" | "location".

Conclusion

To get the union of the keys of a TypeScript interface, we can use the keyof keyword.

Categories
TypeScript Answers

How to assert a type of an HTMLElement in TypeScript?

Sometimes, we want to assert a type of an HTMLElement in TypeScript.

In this article, we’ll look at how to assert a type of an HTMLElement in TypeScript.

How to assert a type of an HTMLElement in TypeScript?

To assert a type of an HTMLElement in TypeScript, we can use brackets or use the as keyword.

For instance, we write

const script: HTMLScriptElement = <HTMLScriptElement>(
  document.getElementsByTagName("script")[0]
);

to cast document.getElementsByTagName("script")[0] to type HTMLScriptElement.

We can also write

const script: HTMLScriptElement = document.getElementsByTagName(
  "script"
)[0] as HTMLScriptElement;

to use as to cast document.getElementsByTagName("script")[0] to type HTMLScriptElement.

Conclusion

To assert a type of an HTMLElement in TypeScript, we can use brackets or use the as keyword.