Type
Basic Types in TypeScript #
You can define types for JavaScript code, such as variables or functions, using TypeScript.
TypeScript primarily has the following 12 basic types:
- Boolean
- Number
- String
- Object
- Array
- Tuple
- Enum
- Any
- Void
- Null
- Undefined
- Never
Primitive Type #
- These are data types that store actual values, not objects or references.
- The ability to use built-in functions for primitive types is due to how JavaScript handles them.
- (As of ES2015) 6 types
- boolean
- number
- string
- symbol
- null
- undefined
boolean #
- The most basic data type.
- Simply a true or false value.
- Called 'boolean' in JS / TS.
number #
- Like JavaScript, all numbers in TypeScript are floating-point values.
- In addition to hexadecimal and decimal literals, TypeScript supports binary and octal literals introduced in ECMAScript 2015.
- NaN
- Notation like 1_000_000 is possible.
string #
- As in other languages, we use the
stringtype to refer to this text format. - Like JavaScript, TypeScript uses double quotes "" and single quotes '' to enclose string data.
Template String #
- Strings that can span multiple lines or embed expressions.
- These strings are enclosed by backtick characters.
- Embedded expressions are used in the form
${expr}.
symbol #
- This is ECMAScript 2015's Symbol.
- Cannot be used with
new Symbol. - You can create a symbol type by using
Symbolas a function. - It is used to hold primitive type values.
- It creates unique and immutable values.
- Therefore, it is often used to control access.
null & undefined #
- In TypeScript,
undefinedandnullactually have their own types:undefinedandnullrespectively. - Similar to
void, they are not particularly useful on their own. - Both exist only in lowercase.
- To allow assigning
undefinedandnull, you must use aunion type.
object #
- A type used when you want to represent
something that is not a primitive type.
array #
- Originally, arrays in JavaScript are objects.
- Usage
- Array
- Type[]
- Array
Tuple #
- An array where the types are not uniform.
- It is also an object.
- Caution is required when extracting and using elements.
- If you destructure the array, the types are correctly inferred.
any #
- A type that can be anything.
- The key is to avoid using it as much as possible.
- This is because type checking does not occur properly at compile time.
- Therefore, some compiler options will throw an error if
anyshould be used but isn't.noImplicitAny
unknown #
- When writing applications, you may need to describe the type of a variable you don't know.
- You might intentionally want these values to accept any value from dynamic content.
- You can narrow down to a more specific variable by performing
typeofchecks, comparison checks, oradvanced type guards.
never #
- Used in return types.
- When used in return types, it mostly covers the following three cases:
The
nevertype is a subtype of all types and can be assigned to all types. However, nothing can be assigned tonever. Not evenanycan be assigned tonever. It is also used to prevent mistakes of inserting incorrect types.
void #
- Represents an empty state that holds no type.
- It has a type but no value.
- It is lowercase.
- Generally used as the return type for functions that do not return a value.
- The only assignable value is
undefined.