Const-Assertions

Const Assertions

Source

Const assertions is a new construct for literal values. Using this TS will take your object and apply the narrowest set of possible types to the object.

Example

type Mode = 'a' | 'b';

type ModeKeys =
  | 'add'
  | 'subtract'

const ModesList = {
  add: 'a',
  subtract: 's'
} as const;

console.log(typeof MaskModesList.add);

TS will look at ModesList and infer the closest type. In this case the ModeKeys and the Mode types

#JavaScript
#TypeScript