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