Categories
TypeScript Answers

How to omit some items from an enum in TypeScript?

Sometimes, we want to omit some items from an enum in TypeScript.

In this article, we’ll look at how to omit some items from an enum in TypeScript.

How to omit some items from an enum in TypeScript?

To omit some items from an enum in TypeScript, we can use the Exclude type.

For instance, we write

enum ErrCode {
  Ok = 0,
  Error = 1,
  AccessDeny = 201,
  PostsNotFound = 202,
  TagNotFound = 203,
}

type ErrorErrcode = Exclude<ErrCode, ErrCode.Ok>;

to use Exclude to exclude the ErrCode.Ok value from the ErrCode type and return that as the type.

Then we assign that to the ErrorErrcode type alias.

Conclusion

To omit some items from an enum in TypeScript, we can use the Exclude type.

Categories
TypeScript Answers

How to define an array of strings in a TypeScript interface?

Sometimes, we want to define an array of strings in a TypeScript interface.

In this article, we’ll look at how to define an array of strings in a TypeScript interface.

How to define an array of strings in a TypeScript interface?

To define an array of strings in a TypeScript interface, we can set a property to a string variable.

For instance, we write

interface Addressable {
  address: string[];
}

to define the Addressable interface with the address property set to a string array with string[].

Conclusion

To define an array of strings in a TypeScript interface, we can set a property to a string variable.

Categories
TypeScript Answers

How to declare the type of ‘this’ in a TypeScript function?

Sometimes, we want to declare the type of ‘this’ in a TypeScript function.

In this article, we’ll look at how to declare the type of ‘this’ in a TypeScript function.

How to declare the type of ‘this’ in a TypeScript function?

To declare the type of ‘this’ in a TypeScript function, we can add the fake this parameter in our functions.

For instance, we write

class Handler {
  info: string;

  onClick(this: Handler, e: Event) {
    this.info = e.message;
  }
}

const h = new Handler();

to add the this parameter into onClick and set it to type Handler.

The parameter will be discarded after it’s compiled.

But it’ll let the TypeScript compiler check for valid properties for this.

Conclusion

To declare the type of ‘this’ in a TypeScript function, we can add the fake this parameter in our functions.

Categories
TypeScript Answers

How to set the TypeScript definitions for all projects files?

Sometimes, we want to set the TypeScript definitions for all projects files.

In this article, we’ll look at how to set the TypeScript definitions for all projects files.

How to set the TypeScript definitions for all projects files?

To set the TypeScript definitions for all projects files, we can add entries to the compilerOptions.lib property in tsconfig.json.

For instance, we write

{
  //...
  "compilerOptions": {
    "target": "ES5",
    "module": "commonjs",
    "lib": ["es5", "es2015.promise", "dom"]
  },
  "include": ["src/**/*.ts"]
  //...
}

to add the compilerOptions.lib option and set it to ["es5", "es2015.promise", "dom"] into tsconfig.json

This will make all the variables provided by the browser and the JavaScript core library of the given versions available in our projects.

Conclusion

To set the TypeScript definitions for all projects files, we can add entries to the compilerOptions.lib property in tsconfig.json.

Categories
TypeScript Answers

How to iterate over interface properties in TypeScript?

Sometimes, we want to iterate over interface properties in TypeScript.

In this article, we’ll look at how to iterate over interface properties in TypeScript.

How to iterate over interface properties in TypeScript?

To iterate over interface properties in TypeScript, we can use the Object.keys method.

For instance, we write

const Activity = {
  id: "",
  title: "",
  body: "",
  json: {},
};

type Activity = typeof Activity;
const headers: Array<Object> = Object.keys(Activity).map((key) => {
  return { text: key, value: key };
});

to define the Activity object.

Then we call Object.keys with Activity to return an array of non-inherited keys in Activity.

And then we call map with a callback that maps each key to the values we want.

We set headers to type Array<Object> so that the TypeScript compiler knows that headers is an array of objects.

Conclusion

To iterate over interface properties in TypeScript, we can use the Object.keys method.