Happylab Logo
Published on

Day06: Exploring TypeScript Data Types

Authors

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.
numberprimitiveDefines a number type.
booleanprimitiveDefines a boolean type.
nullprimitiveDefines a null type, which can also be assigned to all types (not allowed in strict mode).
undefinedprimitiveDefines an undefined type, which can also be assigned to all types (not allowed in strict mode).
objectobjectDefines an object type.
arraysobjectCan be represented using "type + brackets" or array generics.
functionobjectA function has inputs and outputs, and you can specify types for parameters (inputs) and return values (outputs).
anyTSRepresents a type that allows assignment of any type.
unknownTSSimilar to any, but unknown can only be assigned to any and itself.
voidTSRepresents a function that does not return any value.
neverTSRepresents a type that should not exist, generally used for error handling functions.
union typesTSUnion types allow defining values that can be of multiple types.
intersection typesTSIntersection types require values to conform to multiple types.
literal typesTSCertain special "values" can be used as "types."
tupleTSA tuple combines different types into one object.
enumTSEnum 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