Skip to content

Latest commit

 

History

History
38 lines (33 loc) · 764 Bytes

Keyof-"as const".md

File metadata and controls

38 lines (33 loc) · 764 Bytes

Keyof - "as const"

Keyof

  • Key can be String or Numeric.
  • makes a unique identifier/ object, and creates a union between a collection of values.

Documentation

Ex. 1

type Point = { x: number; y: number };
type P = keyof Point;

so, P: x | y

Ex. 2

type Example = {name: string; age: number; 42: boolean;};
type Keys = keyof Example; // "name" | "age" | 42

"as const"

  • makes an array immutable
  • makes an array readonly

Documentation

Ex. 1

const myArray = [1, 2, 3]; //myArray can be changed
const myArray = [1, 2, 3] as const; //myArray cannot be changed

Ex. 2

const person = { 
    name: 'John', 
    age: 30, 
    hobbies: ['reading', 'coding' 'gaming'] as const
};