xxxxxxxxxx
let indexedArray: {[key: string]: number}
let indexedArray: {[key: string]: number} = {
foo: 123,
bar: 456
}
indexedArray['foo'] = 12;
indexedArray.foo= 45;
xxxxxxxxxx
const myObj = { a: 1, b: 'some_string' } as const;
type values = typeof myObj[keyof typeof myObj];
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
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();
}
xxxxxxxxxx
interface MyObjectType {
property1: number;
property2: string;
// Add more properties as needed
}
// Usage example
const myObject: MyObjectType = {
property1: 10,
property2: "Hello world",
// Assign values to other properties
};
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"
xxxxxxxxxx
class Planet
{
name: string;
galaxy: string;
numberOfMoons: number;
weight: number;
}
let earth = new Planet("Earth", "Milky Way", 1, 10000);
let jupiter = new Planet("Jupiter", "Milky Way", 21, 2000000);
TypeScriptCopy
xxxxxxxxxx
numobj:any={}
ngOnInit(){
numobj["positive"]={
"ODD":[1,3,5,7],
"event":[2,4,6,8]
}
console.log(numobj)
}