Categories
TypeScript Answers

How to obtain the return type of a function with TypeScript?

Sometimes, we want to obtain the return type of a function with TypeScript.

In this article, we’ll look at how to obtain the return type of a function with TypeScript.

How to obtain the return type of a function with TypeScript?

To obtain the return type of a function with TypeScript, we can use the ReturnType type.

For instance, we write

type T10 = ReturnType<() => string>;

to get the type of of the () => string function type and assign it to T10.

Therefore T10 is string.

Conclusion

To obtain the return type of a function with TypeScript, we can use the ReturnType type.

Categories
TypeScript Answers

How to import other TypeScript files?

Sometimes, we want to import other TypeScript files.

In this article, we’ll look at how to import other TypeScript files.

How to import other TypeScript files?

To import other TypeScript files, we just use the JavaScript import statement.

For instance, we write

import { ZipCodeValidator } from "./ZipCodeValidator";

const myValidator = new ZipCodeValidator();

to import the ZipCodeValidator constructor from the ZipCodeValidator TypeScript module.

Conclusion

To import other TypeScript files, we just use the JavaScript import statement.

Categories
TypeScript Answers

How to declare a fixed length array in TypeScript?

Sometimes, we want to declare a fixed length array in TypeScript.

In this article, we’ll look at how to declare a fixed length array in TypeScript.

How to declare a fixed length array in TypeScript?

To declare a fixed length array in TypeScript, we can use the Array constructor or create a tuple.

For instance, we write

const arr = new Array<number>(3);
const tuple: [number, number, number] = [1, 2, 3];

to create the arr array which is an array of numbers with new Array<number>(3).

And we create a tuple with the [number, number, number] tuple type which we set to an array with 3 numbers.

Conclusion

To declare a fixed length array in TypeScript, we can use the Array constructor or create a tuple.

Categories
TypeScript Answers

How to define static property in TypeScript interface?

Sometimes, we want to define static property in TypeScript interface.

In this article, we’ll look at how to define static property in TypeScript interface.

How to define static property in TypeScript interface?

To define static property in TypeScript interface, we should put the static property in a class instead.

For instance, we write

class RichDate {
  public static minValue = new Date();
}

to add the minValue static property to the RichDate class with the static keyword.

Then we can access the property by writing

RichDate.minValue 

Conclusion

To define static property in TypeScript interface, we should put the static property in a class instead.

Categories
TypeScript Answers

How to create a TypeScript static class?

Sometimes, we want to create a TypeScript static class.

In this article, we’ll look at how to create a TypeScript static class.

How to create a TypeScript static class?

To create a TypeScript static class, we can create an abstract class.

For instance, we write

export abstract class MyClass {
  public static myProp = "Hello";

  public static doSomething(): string {
    return "World";
  }
}

const x = MyClass.doSomething();

to create the MyClass abstract class with the abstract keyword.

In it, we add the doSomething method.

And we call it with MyClass.doSomething.

Conclusion

To create a TypeScript static class, we can create an abstract class.