TypeScript Less Common Primitives Type
Last Updated :
03 Nov, 2023
TypeScript Less Common Primitives Type offers a rich set of primitive types to represent data. While most developers are familiar with types like number, string, boolean, and symbol, TypeScript also provides less common primitive types that can be incredibly useful in specific scenarios.
These are some Less Common Primitives Types:
bigint
This type represents arbitrary-precision integers. It can hold integers with an arbitrary number of digits, making it useful for working with very large numbers with precision.
Example:
Javascript
const myBigint: bigint = 1234567890123456789012345678901234567890;
console.log(myBigint);
|
Output:
output
Symbol
TypeScript provides a symbol type, which is used for unique identifiers. Symbols are primarily used as object property keys to avoid naming conflicts.
Example:
Javascript
const mySymbol: symbol = Symbol( 'mySymbol' );
const obj = {
[mySymbol]: 'Hello!,Symbol !'
};
console.log(obj[mySymbol]);
|
Output:
Symbol Output
Conclusion: In this article we have seen less common primitive types like bigint, undefined, null, void, never, and unknown provide powerful tools to handle various scenarios with precision. Understanding when and how to use these types can lead to more robust and type-safe code in your TypeScript projects. Explore these types further to leverage the full potential of TypeScript’s static typing.
Share your thoughts in the comments
Please Login to comment...