diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 4cbfe42..5d71242 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -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) diff --git a/src/dev/ts/interfaces.md b/src/dev/ts/interfaces.md new file mode 100644 index 0000000..65257df --- /dev/null +++ b/src/dev/ts/interfaces.md @@ -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`. + +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; +} +``` diff --git a/src/dev/ts/main.md b/src/dev/ts/main.md new file mode 100644 index 0000000..e69de29