xxxxxxxxxx
export const OBJECT = {
FOO: 'foo',
} as const;
export type TObjectKey = keyof typeof OBJECT;
export type TObjectValue = (typeof OBJECT)[TObjectKey];
xxxxxxxxxx
const myObj = { a: 1, b: 'some_string' } as const;
type values = typeof myObj[keyof typeof myObj];
// values = 1 | 'some_string'
xxxxxxxxxx
type Data = {
value: number;
text: string;
};
type textProperty = Data["text"]; //string
//OR
const data = {
value: 123,
text: 'hello'
};
type textProperty = typeof data["text"]; //string
xxxxxxxxxx
const KeyToVal = {
MyKey1: 'myValue1',
MyKey2: 'myValue2',
} as const;
type Keys = keyof typeof KeyToVal;
type Values = typeof KeyToVal[Keys]; // "myValue1" | "myValue2"
xxxxxxxxxx
function isFish(pet: Fish | Bird): pet is Fish {
return (<Fish>pet).swim !== undefined;
}
// Both calls to 'swim' and 'fly' are now okay.
if (isFish(pet)) {
pet.swim();
}
else {
pet.fly();
}