Add ts interfaces

This commit is contained in:
Wilfried OLLIVIER 2020-06-08 20:08:27 +02:00
parent be6d34ff20
commit ea120c0bf5
3 changed files with 63 additions and 0 deletions

View file

@ -16,6 +16,8 @@
- [Testing](./dev/rust/testing.md)
- [Ruby](./dev/ruby/main.md)
- [arrays](./dev/ruby/arrays.md)
- [TypeScript](./dev/ts/main.md)
- [interfaces](./dev/ts/interfaces.md)
- [Web](./dev/web/main.md)
- [front](./dev/web/front/main.md)
- [babel and babel-preset-stage-2](./dev/web/front/babel-preset-stage-2.md)

61
src/dev/ts/interfaces.md Normal file
View file

@ -0,0 +1,61 @@
# Interfaces in TypeScript
[Handbook](https://www.typescriptlang.org/docs/handbook/interfaces.html)
## Overview
Interface in TypeScripts are used to create a _blueprint_ or an abstraction of a class.
An interface can contains properties (_mandatory_, _optional_ or _readonly_)
```typescript
interface Example {
mandatory: string;
optional?: string;
readonly ro: string;
}
```
And methods
```typescript
interface Example {
mandatory: string;
optional?: string;
concat(sep: string): string;
}
```
To use the interface, a class should **implement** it,
```typescript
class Impl implements Example {
mandatory: string;
optional?: string;
constructor(m: string, o: string) {
this.mandatory = m;
this.optional = o;
}
contact(sep: string): string {
return `${this.mandatory}${sep}${this.optional}`;
}
}
```
## The Promise case
What about a promise based method ? Do not forget what async/await does. It wraps all the things in `Promise<T>`.
In order to add promise to your interface, just specify a Promise as return type.
```typescript
interface Example {
mandatory: string;
optional?: string;
promize(arg: string): Promise<string>;
}
```

0
src/dev/ts/main.md Normal file
View file