keys(), values(), entries()
interface ObjectConstructor {
keys(o: object): string[];
}Object.keys() 가 string[] 타입의 값을 반환하는 이유
Object.keys() 가 string[] 타입의 값을 반환하는 이유interface Point {
x: number;
y: number;
}
function fn(key: keyof Point) {
if (key === "x" || key === "y") {
console.log(key);
} else {
throw new Error("Impossible Path");
}
}interface PointWithName extends Point {
name: string;
}
const pointWithName: PointWithName = {
name: "origin",
x: 0,
y: 0
};
((point: Point) => {
for (const key of Object.keys(point)) {
// type 'string' is not assignable to
// parameter of type 'keyof Point'
fn(key);
}
})(pointWithName);Last updated