TypeScript data types
Boolean value
Boolean values are the most basic data type. In TypeScript, the boolean
Boolean value type is defined using:
let isDone: boolean = false; // Compilation passed // The following conventions, code fragments with compilation errors are not emphasized, and the default is to compile and pass
Note that objects created with constructors Boolean
are not booleans:
let createdByNewBoolean: boolean = new Boolean(1); // index.ts(1,5): error TS2322: Type 'Boolean' is not assignable to type 'boolean'. // As agreed later, the code snippet of the compilation error is marked in the comments, indicating that the compilation failed
What is actually new Boolean()
returned is an Boolean
object:
let createdByNewBoolean: Boolean = new Boolean(1);
A direct call Boolean
can also return a boolean
type:
let createdByBoolean: boolean = Boolean(1);
In TypeScript, boolean
it's a primitive type in JavaScript, Boolean
but a constructor in JavaScript. The other basic types (except null
and undefined
) are the same and will not be repeated.
Numerical value
Use to number
define numeric types:
let decLiteral: number = 6; let hexLiteral: number = 0xf00d ; // binary notation in ES6 let binaryLiteral: number = 0b1010; // octal notation in ES6 let octalLiteral: number = 0o744; let notANumber: number = NaN; let infinityNumber: number = Infinity;
Compilation result:
var decLiteral = 6 ; var hexLiteral = 0xf00d ; // binary notation in ES6 var binaryLiteral = 10 ; // octal notation in ES6 var octalLiteral = 484 ; var notANumber = NaN; var infinityNumber = Infinity;
where 0b1010
sum 0o744
is the binary and octal notation in ES6 , which are compiled to decimal numbers.
string
Use the string
definition string type:
let myName: string = 'Tom'; let myAge: number = 25; // Template string let sentence: string = `Hello, my name is ${myName}. I'll be ${myAge + 1} years old next month.`;
Compilation result:
var myName = 'Tom'; var myAge = 25; // 模板字符串 var sentence = "Hello, my name is " + myName + ".\nI'll be " + (myAge + 1) + " years old next month.";
Which is `
used to define template strings in ES6 , ${expr}
used to embed expressions in template strings.
null
JavaScript does not have the concept of void (Void), in TypeScirpt, you can use void
a function that does not have any return value:
function alertName(): void { alert('My name is Tom'); }
Declaring a void
variable of a type doesn't do much, because you can only assign it to undefined
and null
:
let unusable: void = undefined;
Null 和 Undefined
In TypeScript, these two primitive data types can be defined using null
and :undefined
let u: undefined = undefined; let n: null = null;
undefined
Variables of type can only be assigned to undefined
, null
and variables of type can only be assigned to null
.
void
The difference with and is that undefined
and null
are subtypes of all types. That is to say undefined
, a variable of type can be assigned to number
a variable of type:
// This will not report an error let num: number = undefined; // This will also not report an error let u: undefined; let num: number = u;
A variable of void
type cannot be assigned to number
a variable of type:
let u: void; let num: number = u; // index.ts(2,5): error TS2322: Type 'void' is not assignable to type 'number'.
any value type
If it is a normal type, changing the type during assignment is not allowed:
let myFavoriteNumber: string = 'seven'; myFavoriteNumber = 7 ; // index.ts(2,1): error TS2322: Type 'number' is not assignable to type 'string'.
But if it is a any
type, it is allowed to be assigned to any type.
let myFavoriteNumber: any = 'seven';
myFavoriteNumber = 7;
Accessing any property on any value is allowed:
let anyThing: any = 'hello';
console.log(anyThing.myName);
console.log(anyThing.myName.firstName);
It is also allowed to call any method:
let anyThing: any = 'Tom'; anyThing.setName('Jerry'); anyThing.setName('Jerry').sayHello(); anyThing.myName.setFirstName('Cat');
It can be considered that after declaring a variable as an arbitrary value, any operation on it will return the type of the content as an arbitrary value.
A variable is recognized as any value type if it is declared without specifying its type:
let something; something = 'seven'; something = 7; something.setName('Tom');
Equivalent to
let something: any; something = 'seven'; something = 7; something.setName('Tom');
type inference
Although the following code does not specify the type, it will report an error when compiling:
let myFavoriteNumber = 'seven'; myFavoriteNumber = 7 ; // index.ts(2,1): error TS2322: Type 'number' is not assignable to type 'string'.
In fact, it is equivalent to:
let myFavoriteNumber: string = 'seven'; myFavoriteNumber = 7 ; // index.ts(2,1): error TS2322: Type 'number' is not assignable to type 'string'.
TypeScript will infer a type without explicitly specifying a type, which is type inference.
If there is no assignment at the time of definition, regardless of whether there is an assignment later, it will be inferred as a any
type and will not be type checked at all:
let myFavoriteNumber; myFavoriteNumber = 'seven'; myFavoriteNumber = 7;
union type
Union Types indicate that the value can be one of several types.
let myFavoriteNumber: string | number; myFavoriteNumber = 'seven'; myFavoriteNumber = 7 ; let myFavoriteNumber: string | number; myFavoriteNumber = true; // index.ts(2,1): error TS2322: Type 'boolean' is not assignable to type 'string | number'. // Type 'boolean' is not assignable to type 'number'.
Union types are used to |
separate each type.
let myFavoriteNumber: string | number
The implication here is that the allowed myFavoriteNumber
types are string
or number
, but not other types.
When TypeScript is not sure what type a variable of a union type is, we can only access properties or methods that are common to all types of the union type:
function getLength(something: string | number): number { return something.length; } // index.ts(2,22): error TS2339: Property 'length' does not exist on type 'string | number'. // Property 'length' does not exist on type 'number'.
In the above example, it length
is not a common property of string
and number
, so an error will be reported.
Accessing the common properties of string
sums number
is fine:
function getString(something: string | number): string { return something.toString(); }
When a variable of a union type is assigned, a type is inferred according to the rules of type inference:
let myFavoriteNumber: string | number; myFavoriteNumber = 'seven'; console.log(myFavoriteNumber.length); // 5 myFavoriteNumber = 7; console.log(myFavoriteNumber.length); // Error when compiling // index.ts(5,30): error TS2339: Property 'length' does not exist on type 'number'.
In the above example, the second line myFavoriteNumber
is inferred string
, and accessing its length
properties will not throw an error.
The fourth line myFavoriteNumber
is inferred , and an error is reported when number
accessing its length
properties.
refer to:
Getting Started with TypeScript