About
Data Type - Interface (Abstract Type) in Typescript
Implement
In TypeScript, two types are compatible if their internal structure is compatible.
An interface can be implemented just by having the same structure that the interface requires, without an explicit implements clause.
Example
- You can define a interface as type
interface Person {
firstName: string;
lastName: string;
}
- And use it as type declaration
function greeter(person: Person) {
return "Hello, " + person.firstName + " " + person.lastName;
}
- Then create an object of this type and calling the typed function. A user is a person because it has the same structure than the interface declaration.
let user = { firstName: "Jane", lastName: "User" };
document.body.textContent = greeter(user);