JavaScript that scales
TypeScript
ES6
ES5
Lang | Types check | Run time safety |
---|---|---|
|
Dynamic | Weak |
|
Static | Weak |
|
Static | Strong |
No extra memory footprint
Structural
type Foo = {
foo: string;
handleStuff: () => void;
}
type Bar = {
foo: string;
}
let 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;
Nominal
#life
let foo = {
foo: 'foo',
handleStuff: () => ({})
};
const bar = 5;
bar = foo;
foo = bar;
@michalczukm
Senior Software Engineer
Spartez / Atlassian
Jira Cloud
😈
[
{
"id": 1,
"name": "Fancy product",
"type": "stock",
"price": 100,
"amountInStock": 101
},
{
"id": 2,
"name": "Ultimate product",
"type": "marketing",
"price": 200,
"marketingName": "GameChanger2000",
"description": "our new ultimate product!"
},
{
"id": 3,
"name": "Another product",
"type": "stock",
"price": 300,
"amountInStock": 301
}
]
Only for "stock"
product
Only for "marketing"
product
// 😢
type Product = {
id: string,
type: 'stock' | 'marketing',
description?: string, // marketing only
marketingName?: string, // marketing only
price: number,
amountInStock?: number // stock only
};
// 😢
type Client = {
id: string,
name: string,
version: number,
WasCreatedOnDate?: Date, // removed in v2
createdOnUTC?: string, // v2 only
refId?: string // v2 only
};
// this is just internal type, it should never be used outside `Phone`
type PhoneFeatures = {
isWaterproof: boolean,
ramAmount: number,
cpu: string
};
export type Phone = {
id: string,
features: PhoneFeatures,
promotedFeature: 'isWaterproof' | 'ramAmount' | 'cpu'
}
type Phone = {
id: string,
features: {
isWaterproof: boolean,
ramAmount: number,
cpu: string
},
promotedFeature: 'isWaterproof' | 'ramAmount' | 'cpu'
}
function handle(features: Phone['features']) {
features.isWaterproof;
}
(parameter) features: {
isWaterproof: boolean;
ramAmount: number;
cpu: string;
}
[
// one client row
[
{
"id": 1
},
{
"clientId": "100",
"revenue": 12345
},
{
"country": "Poland",
"phoneNumber": "000-000-000"
}
],
...
]
any
unknown
class
type
interface
[
{
"id": 1,
"name": "Client 1",
"continent": "AF"
},
{
"id": 2,
"name": "Client 2",
"continent": "OC"
},
{
"id": 3,
"name": "Client 3",
"continent": "EU"
}
]
Continent
acronym
// 😢
type Client = {
id: string,
name: string,
continent: 'AF' | 'AN' | 'AS' | 'EU' | 'NA' | 'OC' | 'SA';
}
export enum Continent {
Africa = 'AF',
Antarctica = 'AN',
Asia = 'AS',
Europe = 'EU',
NorthAmerica = 'NA',
Oceania = 'OC',
SouthAmerica = 'SA'
}
my-types.d.ts
export enum Continent {
Africa = 'AF',
Antarctica = 'AN',
Asia = 'AS',
Europe = 'EU',
NorthAmerica = 'NA',
Oceania = 'OC',
SouthAmerica = 'SA'
}
my-model.ts
"use strict";
var Continent;
(function (Continent) {
Continent["Africa"] = "AF";
Continent["Antarctica"] = "AN";
Continent["Asia"] = "AS";
Continent["Europe"] = "EU";
Continent["NorthAmerica"] = "NA";
Continent["Oceania"] = "OC";
Continent["SouthAmerica"] = "SA";
})(Continent || (Continent = {}));
compile
compile
declare const PolandISOCode = 'PL';
my-types.d.ts
declare const PolandISOCode = 'PL';
my-model.ts
"use strict";
compile
compile
C(A | B) = C(A) + C(B)
C([A, B]) = C(A) x C(B)
Yes please