Skip to content

Commit 8c63cfc

Browse files
union types and dicriminators
Signed-off-by: ivan katliarchuk <[email protected]>
1 parent 38bc368 commit 8c63cfc

File tree

5 files changed

+137
-1
lines changed

5 files changed

+137
-1
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
help:
2+
@printf "Usage: make [target] [VARIABLE=value]\nTargets:\n"
3+
@grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
4+
5+
run-intersections: ## Run intersections
6+
@deno run intersection.ts
7+
8+
run-type-guards: ## Run type guards
9+
@deno run type-guards.ts
10+
11+
run-disc-unions: ## Run discriminated unions
12+
@deno run disc-union.ts
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// discriminated unions
2+
// https://www.udemy.com/course/understanding-typescript/learn/lecture/16893894#overview
3+
4+
interface Bird {
5+
type: 'bird';
6+
flyingSpeed: number;
7+
}
8+
9+
interface Horse {
10+
type: 'horse';
11+
runningSpeed: number;
12+
}
13+
14+
type Animal = Bird | Hose;
15+
16+
function moveAnimal(animal: Animal) {
17+
let speed;
18+
switch (animal.type) {
19+
case 'bird':
20+
speed = animal.flyingSpeed;
21+
break;
22+
case 'horse':
23+
speed = animal.runningSpeed;
24+
}
25+
console.log('Moving with speed: ' + speed);
26+
}
27+
28+
moveAnimal({ type: 'bird', runningSpeed: 70})
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
type Admin = {
2+
name: string;
3+
privileges: string[];
4+
};
5+
6+
interface Employee {
7+
name: string;
8+
startDate: Date;
9+
};
10+
11+
type ElevatedEmployee = Admin & Employee;
12+
// or
13+
interface IElevatedEmployee extends Employee, Admin { }
14+
15+
const e1: ElevatedEmployee = {
16+
name: 'Max',
17+
privileges: ['create-server'],
18+
startDate: new Date()
19+
}
20+
21+
console.log(e1);
22+
23+
type Combinable = string | number;
24+
type Numeric = number | boolean;
25+
type Universal = Combinable & Numeric;

2-understand/s6-advanced-types/tsconfig.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
/* Basic Options */
44
"target": "ES6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
55
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
6+
"lib": [
7+
"dom",
8+
"es6",
9+
"dom.iterable",
10+
"scripthost"
11+
],
612
// "allowJs": true, /* Allow javascript files to be compiled. */
713
// "checkJs": true, /* Report errors in .js files. */
814
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
@@ -43,7 +49,7 @@
4349
// "typeRoots": [], /* List of folders to include type definitions from. */
4450
// "types": [], /* Type declaration files to be included in compilation. */
4551
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
46-
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
52+
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
4753
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
4854

4955
/* Source Map Options */
@@ -55,6 +61,9 @@
5561
/* Experimental Options */
5662
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
5763
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
64+
"noUnusedLocals": true, /* Report errors on unused locals. */
65+
"noUnusedParameters": true, /* Report errors on unused parameters. */
66+
"noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
5867
},
5968
"exclude": [
6069
"analytics.ts",
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
type Combinable1 = string | number;
3+
type Numeric1 = number | boolean;
4+
type Universal1 = Combinable1 & Numeric1;
5+
6+
function add(a: Combinable1, b: Combinable1) {
7+
if (typeof a === 'string' || typeof b === 'string') {
8+
return a.toString() + b.toString();
9+
}
10+
return a + b;
11+
}
12+
13+
type UnknownEmployee = Employee | Admin;
14+
function printEmployeeInformation(emp: UnknownEmployee) {
15+
console.log('Name: ' + emp.name);
16+
if ('privileges' in emp) {
17+
console.log('Privileges: ' + emp.privileges);
18+
}
19+
if ('startDate' in emp) {
20+
console.log('Start Date: ' + emp.startDate);
21+
}
22+
}
23+
24+
const e1: ElevatedEmployee = {
25+
name: 'Max',
26+
privileges: ['create-server'],
27+
startDate: new Date()
28+
}
29+
30+
printEmployeeInformation(e1);
31+
printEmployeeInformation({name: 'Manu', startDate: new Date()});
32+
33+
class Car {
34+
drive() {
35+
console.log('Driving....');
36+
}
37+
}
38+
39+
40+
class Truck {
41+
drive() {
42+
console.log('Driving a track....');
43+
}
44+
45+
loadCargo(amount: number) {
46+
console.log('Loading cargo ...' + amount);
47+
}
48+
}
49+
50+
type Vehicle = Car | Truck;
51+
const v1 = new Car();
52+
const v2 = new Truck();
53+
54+
function useVehicle(vehicle: Vehicle) {
55+
vehicle.drive();
56+
if (vehicle instanceof Truck) {
57+
vehicle.loadCargo(1000);
58+
}
59+
}
60+
61+
useVehicle(v1);
62+
useVehicle(v2);

0 commit comments

Comments
 (0)