Michał Michalczuk
I am fullstack software developer #dotnet #angular #typescript, IT trainer. Both fascinated and terrified by technology advancement.
Michał Michalczuk
Ciklum / Stibo Systems
infoShare Academy
Michał Michalczuk
TypeScript
ES6
ES5
function getValue() {
switch(Math.random() % 3) {
case 0:
return 'foo';
case 1:
return 5;
case 2:
return { value: '5' };
default:
throw new Error('Ups');
}
}
const result = getValue() + 1;
function getValue(): any {
switch(Math.random() % 3) {
case 0:
return 'foo';
case 1:
return 5;
case 2:
return { value: '5' };
default:
throw new Error('Ups');
}
}
const result = getValue() as number + 1;
Lang | Types check | Run time safety |
---|---|---|
JavaScript | Dynamic | Weak |
TypeScript | Static | Weak |
C# / Java | Static | Strong |
type Order = {
clientName: string
};
const orderWorks = {} as Order;
const orderAlsoWorks = <Order>{};
I'm sure of the type, hold my bear
type Foo = {
foo: string;
handleStuff: () => void;
}
type Bar = {
foo: string;
}
const foo: Foo = {
foo: 'foo',
handleStuff: () => ({})
};
// works, struc. of `Bar` is subset
const bar: Bar = foo;
// won't compile.
// `handleStuff` is missing
foo = bar;
class Foo {
public string Foo { get; set; }
}
class Bar {
public string Foo { get; set; }
}
Foo foo = new Foo { Foo = "foo" };
// won't compile.
// Its different type
Bar bar = foo;
function handle( item: {value: string} ) {
// do stuff
}
handle({
value: 'some value'
});
const item = {
name: 'Item name',
value: 'some value',
};
handle(item);
Works
function handle( item: {value: string} ) {
// do stuff
}
// [error] Object literal may only specify known properties,
// and 'name' does not exist
// in type '{ value: string; }'.
handle({
name: 'Item name',
value: 'some value',
});
Error
Class when need to
Interface/Type when need to
describe contract
do anything else
By Michał Michalczuk
Angular 3city meetup, 09.05.2018 Gdańsk
I am fullstack software developer #dotnet #angular #typescript, IT trainer. Both fascinated and terrified by technology advancement.