2024-05-06 20:40:05 +00:00
|
|
|
---
|
2024-10-30 17:34:11 +00:00
|
|
|
date: 2020-09-29
|
2024-05-06 20:40:05 +00:00
|
|
|
id: 1bdd832a-3dee-4b31-9192-c51cde8a4b66
|
|
|
|
title: TypeScript Interfaces
|
|
|
|
---
|
|
|
|
|
|
|
|
# Example
|
|
|
|
|
|
|
|
``` typescript
|
|
|
|
interface Name {
|
|
|
|
first: string;
|
|
|
|
second: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
var name: Name;
|
|
|
|
name = {
|
|
|
|
first: 'John',
|
|
|
|
second: 'Doe'
|
|
|
|
};
|
|
|
|
|
|
|
|
name = { // Error : `second` is missing
|
|
|
|
first: 'John'
|
|
|
|
};
|
|
|
|
name = { // Error : `second` is the wrong type
|
|
|
|
first: 'John',
|
|
|
|
second: 1337
|
|
|
|
};
|
|
|
|
```
|
|
|
|
|
|
|
|
## Class implementing interface
|
|
|
|
|
|
|
|
``` typescript
|
|
|
|
interface Point {
|
|
|
|
x: number; y: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
class MyPoint implements Point {
|
|
|
|
x: number; y: number; // Same as Point
|
|
|
|
}
|
|
|
|
```
|