Primitive Types
TypeScript provides several primitive types that represent the most basic forms of data you can work with. These include string, number, boolean, bigint, symbol, and undefined. You use these types to describe simple values and to help TypeScript catch mistakes in your code.
1234567891011121314151617// String type let firstName: string = "Alice"; // Number type let age: number = 30; // Boolean type let isActive: boolean = true; // BigInt type let bigNumber: bigint = 9007199254740991n; // Symbol type let uniqueId: symbol = Symbol("id"); // Undefined type let notAssigned: undefined = undefined;
The string type is used for text data. In the example above, firstName is declared as a string and assigned the value "Alice". This means firstName can only hold text values.
The number type covers all numeric values, including integers and floating-point numbers. The variable age is a number and is set to 30. You can use number for calculations, measurements, and any other numeric data.
For logical values, TypeScript uses the boolean type. The variable isActive is a boolean and is assigned true. You use boolean to represent true/false or yes/no conditions in your code.
The bigint type is for very large integers that cannot be represented by the regular number type. The variable bigNumber is a bigint, which you create by adding an n to the end of the number. Use bigint when you need to work with extremely large whole numbers.
Symbols are unique and immutable values, and are represented by the symbol type. The variable uniqueId is a symbol, created using the Symbol() function. Symbols are often used as unique keys for object properties.
Finally, undefined is a type that represents a variable that has not been assigned a value. The variable notAssigned is explicitly set to undefined. In TypeScript, a variable that is declared but not initialized will also have the type undefined by default.
By using these primitive types, you can write safer TypeScript code and take advantage of strong type checking to prevent common errors.
Obrigado pelo seu feedback!
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
Can you explain the difference between `bigint` and `number` in more detail?
What are some common use cases for the `symbol` type?
Are there any other primitive types in TypeScript besides these?
Awesome!
Completion rate improved to 8.33
Primitive Types
Deslize para mostrar o menu
TypeScript provides several primitive types that represent the most basic forms of data you can work with. These include string, number, boolean, bigint, symbol, and undefined. You use these types to describe simple values and to help TypeScript catch mistakes in your code.
1234567891011121314151617// String type let firstName: string = "Alice"; // Number type let age: number = 30; // Boolean type let isActive: boolean = true; // BigInt type let bigNumber: bigint = 9007199254740991n; // Symbol type let uniqueId: symbol = Symbol("id"); // Undefined type let notAssigned: undefined = undefined;
The string type is used for text data. In the example above, firstName is declared as a string and assigned the value "Alice". This means firstName can only hold text values.
The number type covers all numeric values, including integers and floating-point numbers. The variable age is a number and is set to 30. You can use number for calculations, measurements, and any other numeric data.
For logical values, TypeScript uses the boolean type. The variable isActive is a boolean and is assigned true. You use boolean to represent true/false or yes/no conditions in your code.
The bigint type is for very large integers that cannot be represented by the regular number type. The variable bigNumber is a bigint, which you create by adding an n to the end of the number. Use bigint when you need to work with extremely large whole numbers.
Symbols are unique and immutable values, and are represented by the symbol type. The variable uniqueId is a symbol, created using the Symbol() function. Symbols are often used as unique keys for object properties.
Finally, undefined is a type that represents a variable that has not been assigned a value. The variable notAssigned is explicitly set to undefined. In TypeScript, a variable that is declared but not initialized will also have the type undefined by default.
By using these primitive types, you can write safer TypeScript code and take advantage of strong type checking to prevent common errors.
Obrigado pelo seu feedback!