type an object in typescript

To type an object in TypeScript, you can define an interface that describes the shape of the object. Here is an example:

index.ts
// Define an interface for the object
interface Person {
    name: string;
    age: number;
    isStudent: boolean;
}

// Create an object of type Person
let person: Person = {
    name: 'Alice',
    age: 30,
    isStudent: false
};
233 chars
14 lines

In this example, we've created an interface called Person that defines the properties name, age, and isStudent with their respective types. Then we've created an object person that conforms to this interface.

related categories

gistlibby LogSnag