3 min read

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:

  1. Primitive Types: string, number, boolean, null, undefined
  2. Object Types: object, arrays, function
  3. 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.

TypeType ClassificationNotesExample
stringprimitiveDefines a string type.Day06: Exploring TypeScript Data Types
numberprimitiveDefines a number type.Day06: Exploring TypeScript Data Types
booleanprimitiveDefines a boolean type.Day06: Exploring TypeScript Data Types
nullprimitiveDefines a null type, which can also be assigned to all types (not allowed in strict mode).Day06: Exploring TypeScript Data Types
undefinedprimitiveDefines an undefined type, which can also be assigned to all types (not allowed in strict mode).Day06: Exploring TypeScript Data Types
objectobjectDefines an object type.Day06: Exploring TypeScript Data Types
arraysobjectCan be represented using "type + brackets" or array generics.Day06: Exploring TypeScript Data Types
functionobjectA function has inputs and outputs, and you can specify types for parameters (inputs) and return values (outputs).Day06: Exploring TypeScript Data Types
anyTSRepresents a type that allows assignment of any type.Day06: Exploring TypeScript Data Types
unknownTSSimilar to any, but unknown can only be assigned to any and itself.Day06: Exploring TypeScript Data Types
voidTSRepresents a function that does not return any value.Day06: Exploring TypeScript Data Types
neverTSRepresents a type that should not exist, generally used for error handling functions.Day06: Exploring TypeScript Data Types
union typesTSUnion types allow defining values that can be of multiple types.Day06: Exploring TypeScript Data Types
intersection typesTSIntersection types require values to conform to multiple types.Day06: Exploring TypeScript Data Types
literal typesTSCertain special "values" can be used as "types."Day06: Exploring TypeScript Data Types
tupleTSA tuple combines different types into one object.Day06: Exploring TypeScript Data Types
enumTSEnum types can be used to manage multiple constants of the same series, used for state judgment.Day06: Exploring TypeScript Data Types

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

Comments