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
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
/*They can be anonymous*/
function greet(person: { name: string; age: number }) {
return "Hello " + person.name;
}
/*or named using an interface*/
interface Person {
name: string;
age: number;
}
function greet(person: Person) {
return "Hello " + person.name;
}
/*or with a type alias*/
type Person = {
name: string;
age: number;
};
function greet(person: Person) {
return "Hello " + person.name;
}
xxxxxxxxxx
const person:{name:string,salary:number, isHeGoodPerson:boolean}={
name: "Abir",
salary: 1200,
isHeGoodPerson: true
}
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