Categories
TypeScript Answers

How to implement sleep function in TypeScript?

Sometimes, we want to implement sleep function in TypeScript.

In this article, we’ll look at how to implement sleep function in TypeScript.

How to implement sleep function in TypeScript?

To implement sleep function in TypeScript, we can create a function that returns a promise that calls setTimeout.

For instance, we write

const delay = (ms: number) => {
  return new Promise((resolve) => setTimeout(resolve, ms));
};

to create the delay function that returns a promise that calls resolve after ms milliseconds is up.

We create the promise using the Promise constructor with a callback that calls setTimeout as the argument.

Then we can use it by writing

(async () => {
  console.log("before delay");
  await delay(1000);
  console.log("after delay");
})();

We use await to log 'after delay' only after the delay function’s returned promise is resolved.

Conclusion

To implement sleep function in TypeScript, we can create a function that returns a promise that calls setTimeout.

Categories
TypeScript Answers

How to use the is keyword do in TypeScript?

Sometimes, we want to use the is keyword do in TypeScript.

In this article, we’ll look at how to use the is keyword do in TypeScript.

How to use the is keyword do in TypeScript?

To use the is keyword do in TypeScript, we can use it in the return type for type guard functions.

For instance, we write

const isString = (test: any): test is string => {
  return typeof test === "string";
};

const example = (foo: any) => {
  if (isString(foo)) {
    console.log(foo.length);
  }
};
example("hello world");

to create the isString type guard function that check whether test is a string.

test is string is the returned type and it’s true if test is a string and false otherwise.

In example, we call isString with foo to check if foo is a string.

If it returns true, then we can use string properties like length safely.

Conclusion

To use the is keyword do in TypeScript, we can use it in the return type for type guard functions.

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.