Add some type examples
This commit is contained in:
parent
bcb5a1535c
commit
10c1e555eb
1
Notes.md
1
Notes.md
|
|
@ -221,7 +221,6 @@ Unsortiert:
|
|||
* Readonly
|
||||
* union
|
||||
* intersection
|
||||
* type vs interface
|
||||
* typeof
|
||||
* keyof
|
||||
* `as` 
|
||||
|
|
|
|||
BIN
img/bee-type.png
Normal file
BIN
img/bee-type.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 36 KiB |
BIN
img/bobbycar.png
Normal file
BIN
img/bobbycar.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 121 KiB |
BIN
img/hovercraft.png
Normal file
BIN
img/hovercraft.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 659 KiB |
BIN
img/hubschraubär.png
Normal file
BIN
img/hubschraubär.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 222 KiB |
19
live/types.ts
Normal file
19
live/types.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
type BeeRole = 'worker'|'queen'|'guard';
|
||||
type Months = number;
|
||||
type Bee = {
|
||||
role: BeeRole,
|
||||
name: string,
|
||||
age: Months,
|
||||
};
|
||||
const bee: Bee = {
|
||||
role: "guard",
|
||||
name: 'Juergen',
|
||||
age: 2,
|
||||
}
|
||||
|
||||
const userNames = {
|
||||
'652a4707-3043-4ca9-b417-24feac0f5953': 'Bernhard',
|
||||
'017daef7-e1f1-4160-81ff-e5b17e86fc6b': 'Frieda',
|
||||
};
|
||||
|
||||
type UserNameDictionary = Record<string, string>;
|
||||
275
workshop.md
275
workshop.md
|
|
@ -377,4 +377,277 @@ export function getFileExtension(path: string): string;
|
|||
|
||||
---
|
||||
|
||||
## Welche Types gibt es?
|
||||
## Wie beschreibe ich nun meine Daten?
|
||||
|
||||
```js
|
||||
const workerBee = {
|
||||
role: 'worker',
|
||||
name: 'Larry',
|
||||
age: 15,
|
||||
};
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Objekte
|
||||
|
||||
```ts
|
||||
const workerBee: Bee = {
|
||||
role: 'worker',
|
||||
name: 'Larry',
|
||||
age: 15,
|
||||
};
|
||||
```
|
||||
|
||||
```ts
|
||||
type Bee = {
|
||||
role: string,
|
||||
name: string,
|
||||
age: number,
|
||||
};
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Semantische Typen
|
||||
|
||||
```ts
|
||||
const workerBee: Bee = {
|
||||
role: 'worker',
|
||||
name: 'Larry',
|
||||
age: 15,
|
||||
};
|
||||
```
|
||||
|
||||
```ts
|
||||
type Bee = {
|
||||
role: BeeRole,
|
||||
name: string,
|
||||
age: Months,
|
||||
};
|
||||
type BeeRole = 'worker'|'queen'|'guard';
|
||||
type Months = number;
|
||||
```
|
||||
|
||||
---
|
||||
|
||||

|
||||
|
||||
---
|
||||
|
||||
## Dictionaries
|
||||
|
||||
```js
|
||||
const userNames = {
|
||||
'652a4707-3043-4ca9-b417-24feac0f5953': 'Bernhard',
|
||||
'017daef7-e1f1-4160-81ff-e5b17e86fc6b': 'Frieda',
|
||||
};
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Dictionaries
|
||||
|
||||
```ts
|
||||
const userNames: UserNameDictionary = {
|
||||
'652a4707-3043-4ca9-b417-24feac0f5953': 'Bernhard',
|
||||
'017daef7-e1f1-4160-81ff-e5b17e86fc6b': 'Frieda',
|
||||
};
|
||||
type UserId = string;
|
||||
type UserNameDictionary = Record<UserId, string>;
|
||||
```
|
||||
---
|
||||
|
||||
## Dictionaries (alternative)
|
||||
|
||||
```ts
|
||||
const userNames: UserNameDictionary = {
|
||||
'652a4707-3043-4ca9-b417-24feac0f5953': 'Bernhard',
|
||||
'017daef7-e1f1-4160-81ff-e5b17e86fc6b': 'Frieda',
|
||||
};
|
||||
type UserId = string;
|
||||
type UserNameDictionary = {
|
||||
[UserId]: string;
|
||||
};
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Optionale Properties
|
||||
|
||||
```js
|
||||
const lkw = {
|
||||
anzahlRaeder: 18,
|
||||
hubraum: 25,
|
||||
};
|
||||
const bobbycar = {
|
||||
anzahlRaeder: 4,
|
||||
};
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Optionale Properties
|
||||
|
||||
```ts
|
||||
const lkw: Car = {
|
||||
anzahlRaeder: 18,
|
||||
hubraum: 25,
|
||||
};
|
||||
const bobbycar: Car = {
|
||||
anzahlRaeder: 4,
|
||||
};
|
||||
type Kubikmeter = number;
|
||||
type Car = {
|
||||
anzahlRaeder: number,
|
||||
hubraum?: Kubikmeter,
|
||||
};
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Nur optionale Properties
|
||||
|
||||
```ts
|
||||
type Vehicle = Partial<{
|
||||
anzahlRaeder: number,
|
||||
hubraum: Kubikmeter,
|
||||
anzahlKufen: number,
|
||||
}>;
|
||||
```
|
||||

|
||||

|
||||

|
||||
|
||||
---
|
||||
|
||||
## Nur optionale Properties
|
||||
|
||||
```ts
|
||||
type Vehicle = Partial<{
|
||||
anzahlRaeder: number,
|
||||
hubraum: Kubikmeter,
|
||||
anzahlKufen: number,
|
||||
}>;
|
||||
const helikopter: Vehicle = {
|
||||
anzahlKufen: 2,
|
||||
};
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Nur optionale Properties
|
||||
|
||||
```ts
|
||||
type Vehicle = Partial<{
|
||||
anzahlRaeder: number,
|
||||
hubraum: Kubikmeter,
|
||||
anzahlKufen: number,
|
||||
}>;
|
||||
const helikopter: Vehicle = {
|
||||
anzahlKufen: 2,
|
||||
};
|
||||
const bobbyCar: Vehicle = {
|
||||
anzahlRaeder: 4,
|
||||
};
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Nur optionale Properties
|
||||
|
||||
```ts
|
||||
type Vehicle = Partial<{
|
||||
anzahlRaeder: number,
|
||||
hubraum: Kubikmeter,
|
||||
anzahlKufen: number,
|
||||
}>;
|
||||
const helikopter: Vehicle = {
|
||||
anzahlKufen: 2,
|
||||
};
|
||||
const bobbyCar: Vehicle = {
|
||||
anzahlRaeder: 4,
|
||||
};
|
||||
const luftKissenFahrzeug: Vehicle = {
|
||||
hubraum: 10,
|
||||
};
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Readonly
|
||||
|
||||
```ts
|
||||
// @iserv/colors.ts
|
||||
export type Color = string;
|
||||
export type Colors = Record<'RED'|'GREEN'|'BLUE', Color>
|
||||
export const colors: Colors = {
|
||||
RED: '#ff0000',
|
||||
GREEN: '#00ff00',
|
||||
BLUE: '#0000ff',
|
||||
};
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Readonly
|
||||
|
||||
```ts
|
||||
// @iserv/colors.ts
|
||||
export type Color = string;
|
||||
export type Colors = Record<'RED'|'GREEN'|'BLUE', Color>
|
||||
export const colors: Colors = {
|
||||
RED: '#ff0000',
|
||||
GREEN: '#00ff00',
|
||||
BLUE: '#0000ff',
|
||||
};
|
||||
```
|
||||
```ts
|
||||
// consumer.ts
|
||||
import {colors} from '@iserv/colors.ts';
|
||||
colors = {}; // verboten wegen const
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Readonly
|
||||
|
||||
```ts
|
||||
// @iserv/colors.ts
|
||||
export type Color = string;
|
||||
export type Colors = Record<'RED'|'GREEN'|'BLUE', Color>
|
||||
export const colors: Colors = {
|
||||
RED: '#ff0000',
|
||||
GREEN: '#00ff00',
|
||||
BLUE: '#0000ff',
|
||||
};
|
||||
```
|
||||
```ts
|
||||
// consumer.ts
|
||||
import {colors} from '@iserv/colors.ts';
|
||||
colors = {}; // verboten wegen const
|
||||
colors.BLUE = colors.RED; // erlaubt 😢
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Readonly
|
||||
|
||||
```ts
|
||||
// @iserv/colors.ts
|
||||
export type Color = string;
|
||||
export type Colors = Record<'RED'|'GREEN'|'BLUE', Color>
|
||||
export const colors: Readonly<Colors> = {
|
||||
RED: '#ff0000', ^^^^^^^^
|
||||
GREEN: '#00ff00',
|
||||
BLUE: '#0000ff',
|
||||
};
|
||||
```
|
||||
```ts
|
||||
// consumer.ts
|
||||
import {colors} from '@iserv/colors.ts';
|
||||
colors = {}; // verboten wegen const
|
||||
colors.BLUE = colors.RED; // verbiet 😎
|
||||
```
|
||||
|
||||
---
|
||||
Loading…
Reference in New Issue
Block a user