Skip to main content

【TS 类型操作之 keyof】

keyof

const myObj = {
a: 1,
b: 2,
c: 3,
}
const objKeys = <Obj>(obj: Obj): (keyof Obj)[] => {
return Object.keys(obj) as (keyof Obj)[]
}
objKeys(myObj).map((el) => el)
Object.keys(myObj).map((el) => el)

function getFromObject<MyOBject, Key extends keyof MyOBject>(
myObject: MyOBject,
myKey: Key
): MyOBject[Key] {
return {} as any
}
let result: number = getFromObject(myObj, 'c')