2021-08-11 typescript 学习笔记

keyof

keyof用于获取对象的key

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

type KPoint = keyof Point; // x | y

typeof


const point = {
a : 1,
b: 'test'
}
type P = keyof typeof point; // type '"a" || "b"'

const coordinate: P = 'z' // Type '"z"' is not assignable to type '"a" | "b"'.

你可能感兴趣的:(2021-08-11 typescript 学习笔记)