xxxxxxxxxx
const KeyToVal = {
MyKey1: 'myValue1',
MyKey2: 'myValue2',
} as const;
type Keys = keyof typeof KeyToVal;
type Values = typeof KeyToVal[Keys]; // "myValue1" | "myValue2"
xxxxxxxxxx
const myObj = { a: 1, b: 'some_string' } as const;
type values = typeof myObj[keyof typeof myObj];
// values = 1 | 'some_string'
xxxxxxxxxx
const data = {
a: "first",
b: "second",
};
const values = Object.keys(data).map(key => data[key]);
const commaJoinedValues = values.join(",");
console.log(commaJoinedValues);
xxxxxxxxxx
export const OBJECT = {
FOO: 'foo',
} as const;
export type TObjectKey = keyof typeof OBJECT;
export type TObjectValue = (typeof OBJECT)[TObjectKey];
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 satisfies = <T,>() => <U extends T>(u: U) => u;
const mapper1 = satisfies<Record<string, Type1>>()({ foo1: bar1, foo2: bar2 });
const mapper2 = satisfies<Record<string, Type2>>()({ foo3: bar3, foo4: bar4 });
export type MapperKeys = keyof typeof mapper1 | keyof typeof mapper2;
// type MapperKeys = "foo1" | "foo2" | "foo3" | "foo4"