Type คือข้อมูลชนิดหนึ่ง
ประเภทของ Type
Primitive types
type MyNumber = number;
type MyString = string;
type MyBoolean = boolean;
type MySymbol = symbol;
type MyBigInt = bigint;
type MyUndefined = undefined;
type MyNull = null;Literal types
Literal types คือ Type ที่มีค่านั้น เพียงค่าเดียว
type MyNumberLiteral = 20;
type MyStringLiteral = 'Hello';
type MyBooleanLiteral = true;Data structures (Object Types)
type MyObject = {
key1: boolean;
key2: number;
};
type MyRecord = {
[key: string]: number;
};
type MyTuple = [boolean, number];
type MyArray = number[];Unions
type MyUnion = 'Yes' | 'No';
// Example
const myUnion1: MyUnion = 'Yes';
const myUnion2: MyUnion = 'No';
const myUnion3: MyUnion = 'Maybe'; // ErrorIntersections
type MyIntersection = { count: number } & { title: string };
// Example
const myIntersection1: MyIntersection = {
count: 1,
title: 'Hello',
};
อ้างอิง: https://type-level-typescript.com/types-are-just-data