Day06: Exploring TypeScript Data Types
In the previous article, we learned about the three methods of specifying types. What types can we use? Today, let's organize the data types that TypeScript might use.
Day06: Exploring TypeScript Data Types
In the previous article, we learned about the three methods of specifying types. What types can we use? Today, let's organize the data types that TypeScript might use.
Data Type Classification
I roughly categorize them into three main types:
- Primitive Types: string, number, boolean, null, undefined
- Object Types: object, arrays, function
- Types unique to TypeScript: any, unknown, void, never, union types, intersection types, literal types, tuple, enums
TypeScript Data Type Summary Table
I spent quite a bit of time referencing websites and roughly organized simple usage methods for all types, but I haven't actually applied them in a project yet. I'll take notes while learning (please feel free to correct me if there are any mistakes). In the next few articles, I will introduce each type in detail.
| Type | Type Classification | Notes | Example |
|---|---|---|---|
| string | primitive | Defines a string type. | ![]() |
| number | primitive | Defines a number type. | ![]() |
| boolean | primitive | Defines a boolean type. | ![]() |
| null | primitive | Defines a null type, which can also be assigned to all types (not allowed in strict mode). | ![]() |
| undefined | primitive | Defines an undefined type, which can also be assigned to all types (not allowed in strict mode). | ![]() |
| object | object | Defines an object type. | ![]() |
| arrays | object | Can be represented using "type + brackets" or array generics. | ![]() |
| function | object | A function has inputs and outputs, and you can specify types for parameters (inputs) and return values (outputs). | ![]() |
| any | TS | Represents a type that allows assignment of any type. | ![]() |
| unknown | TS | Similar to any, but unknown can only be assigned to any and itself. | ![]() |
| void | TS | Represents a function that does not return any value. | ![]() |
| never | TS | Represents a type that should not exist, generally used for error handling functions. | ![]() |
| union types | TS | Union types allow defining values that can be of multiple types. | ![]() |
| intersection types | TS | Intersection types require values to conform to multiple types. | ![]() |
| literal types | TS | Certain special "values" can be used as "types." | ![]() |
| tuple | TS | A tuple combines different types into one object. | ![]() |
| enum | TS | Enum types can be used to manage multiple constants of the same series, used for state judgment. | ![]() |
Let's embark on the TypeScript data type adventure together, and remember to wear your masks 😷 and fasten your seatbelts. Let's go! For examples, refer to this link.
References
https://www.typescriptlang.org/docs/handbook/2/basic-types.html https://willh.gitbook.io/typescript-tutorial/basics/primitive-data-types https://ithelp.ithome.com.tw/articles/10223315 https://ithelp.ithome.com.tw/articles/10217384
Related Posts
Day30 :I finally finished!
I finally finished! Recently, I went to draw, and here is a drawing of "The Little Prince and the Fox." My favorite part of "The Little Prince" is their story. Drawing is really therapeutic, just like the feeling of finishing this challenge.
Day29 :TypeScript Learning : React + TypeScript Todo App Part2
Continuing with the Todo app part 2, I will document the issues encountered during the implementation.
Day28 :TypeScript Learning : React + TypeScript Todo App Part1
After taking so many notes, it's finally time to implement a simple to-do app and document the issues encountered during the implementation.
















