TypeScript is just like ES2015 with type-checking. All ES2015 (classes, etc) should work.
any
void
boolean
number
string
null
undefined
string[] /* or Array<string> */
[string, number] /* tuple */
string | null | undefined /* union */
never /* unreachable */
enum Color {Red, Green, Blue = 4}
let c: Color = Color.Green
let isDone: boolean
let isDone: boolean = false
function add (a: number, b: number): number {
return a + b
}
// Return type is optional
function add (a: number, b: number) { ... }
let len: number = (input as string).length
let len: number = (<string> input).length /* not allowed in JSX */
function object(this: {a: number, b: number}, a: number, b: number) {
this.a = a;
this.b = b;
return this;
}
// this is used only for type declaration
let a = object(1,2);
// a has type {a: number, b: number}
function printLabel (options: { label: string }) {
console.log(options.label)
}
// Note the semicolon
function getUser (): { name: string; age?: number } {
}
interface LabelOptions {
label: string
}
function printLabel(options: LabelOptions) { ... }
interface User {
name: string,
age?: number
}
interface User {
readonly name: string
}
{
[key: string]: Object[]
}
type Name = string | string[]
interface User { ... }
function getUser(callback: (user: User) => any) { callback({...}) }
getUser(function (user: User) { ... })
class Point {
x: number
y: number
static instances = 0
constructor(x: number, y: number) {
this.x = x
this.y = y
}
}
class Point {...}
class Point3D extends Point {...}
interface Colored {...}
class Pixel extends Point implements Colored {...}
class Point {
static instances = 0;
constructor(
public x: number,
public y: number,
){}
}
class Point {
public someUselessValue!: number;
...
}
class Greeter<T> {
greeting: T
constructor(message: T) {
this.greeting = message
}
}
let greeter = new Greeter<string>('Hello, world')
export interface User { ... }
interface Building {
room: {
door: string,
walls: string[],
};
}
type Walls = Building['room']['walls']; // string[]
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️