let isDone: boolean = false;
typeof isDone === 'boolean' //true
// Type 'boolean' is assignable to type 'Boolean'.
let isOk: Boolean = true;
// Type 'Boolean' is not assignable to type 'boolean'.
// 'boolean' is a primitive, but 'Boolean' is a wrapper object.
// Prefer using 'boolean' when possible.
let isNotOk: boolean = new Boolean(true);
--strictNullChecks
사용하면, null 과 undefined 는 void 나 자기 자신들에게만 할당할 수 있다.let name: string = null;
let age: number = undefined;
// strictNullChecks => true
// Type 'null' is not assignable to type 'string'.
let name: string = null; (x)
// null => null || void, undefined => undefined || void
// Type 'null' is not assignable to type 'undefined'.
let u: undefined = null; // (x)
let v: void = undefined; // (o)
let union: string | null | undefined = 'str';
ECMAScript2015 의 Symbol.
primitive 의 값을 담아서 사용.
고유하고 수정불가능한 값으로 만들어준다. (주로 접근을 제어하는 데 쓰는 경우가 많다.)
let sym = symbol();
let obj = {
[sym]: "value"
};
console.log(obj[sym]); // "value"
let list: number[] = [1,2,3];
let list: Array<number> = [4,5,6];
function returnVoid(message): void {
console.log(message);
}
returnVoid('리턴이 없다');
// Function returning never must have unreachable end point
function error(message: string): never {
throw new Error(message);
}
// Inferred return type is never
function fail() {
return error("Something failed")
}
// Function returning never must have unreachable end poing
function infiniteLoop(): never {
while (true) {
}
}
enum Color {Red, Green, Blue}
let c: Color = Color.Green;
enum Color {Red = 1, Green, Blue}
let c: Color = Color.Green;
enum Color {Red = 1, Green = 2, Blue = 4}
let c: Color = Color.Green;
enum Color {Red = 1, Green, Blue}
let colorName: string = Color[2];
// Declare a tuple type
let x: [string, number];
// Initialize it
x = ['hello', 10]; // OK
x = [10, 'hello']; // Error
x[3] = 'world'; // OK, 'string' can be assigned to 'string | number'
console.log(x[5].toString()); // OK, 'string' and 'number' both have 'toString'
x[6] = true // Error, 'boolean' isn't 'string | number'