Primitive data type

There are two types of JavaScript: primitive data types and object types.

It should be mentioned here that the original data type has a new type Symbol in ES6 and a new type BigInt in ES10.

Boolean value

In TypeScript, Boolean is used to define Boolean types:

let isDone:boolean = false;
Copy the code

Note that the constructor Boolean creates an object that is not a Boolean. It returns an object

let createdByNewBoolean: Boolean = new Boole4an(1); 
Copy the code

In TypeScript, Boolean is the basic type in JavaScript, and binary Boolean is the constructor in JavaScript. The other basic types (except null and undefined) are left out.

The numerical

Define numeric types using number:

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.`;
Copy the code

Where ´ defines the template string in ES6 and ${expr} is used to embed expressions in the template character.

A null value

JavaScript does not use the concept of void values. In Typescript, we use void to represent functions that have no value put back:

function alertName() :void {
    alert('My name is Jone');
}
Copy the code

Null, and Undefined

In TypeScript, null and undefined are used to define these two primitive data types:

let u: undefined = undefined;
let n:null = null;
Copy the code

The difference with void is that undefined and null are subtypes of all types. Variables of type undefined can be assigned to variables of type number:

let u: undefined;
let num: number = u;
Copy the code

Variables of type void cannot be assigned to variables of type number

Any value

An arbitrary value (Any) is used to indicate that assignments of Any type are allowed.

What is an arbitrary value type

If it is a common type, it is not allowed to change the type during assignment:

let myFavoriteNumber: string = 'seven';
myFavoriteNumber =7
//Type 'number' is not assignable to type 'string'.
Copy the code

Type number cannot be assigned to string, so when we use any:

    let myFavoriteNumber: any = 'seven';
    myFavoriteNumber =7

Copy the code

Properties and methods of arbitrary values

It is possible to access any property on any value using any method

let anyThing: any = 'hello';
console.log(anyThing.myName);
console.log(anyThing.myName.firstName);

let anyThing: any = 'Tom';
anyThing.setName('Jerry');
anyThing.setName('Jerry').sayHello();
anyThing.myName.setFirstName('Cat');
Copy the code

As you can see from the above code, after declaring a variable as an arbitrary value, any operation on it returns content of the type of any value.

A variable of undeclared type

If the chi type is not specified at the time of life, the variable will be identified as any value type:

let something;
something = 'seven';
something = 7;

something.setName('Tom');
Copy the code

Type inference

If no type is explicitly specified, TypeScript infers a type according to the rules of type inference.

What is type inference

The following code does not specify a type, but will report an error at compile time:

let myFavoriteNumber = 'seven';
myFavoriteNumber = 7;
//Type 'number' is not assignable to type 'string'.
Copy the code

TypeScript extrapolates a type when no type is explicitly specified. This is called type inference, but if no value is assigned, it is inferred to be of type any and not checked at all:

let myFavoriteNumber;
myFavoriteNumber = 'seven';
myFavoriteNumber = 7;
Copy the code

The joint type

The union type indicates that the value can be one of many types.

    let myFavoriteNumber: string | number;
    myFavoriteNumber = 'seven';
    myFavoriteNumber = 7;
Copy the code

Joint type using | separated for each type. The above code allows the myFavoriteNumber type to be string or number, but not any other type. When a variable of the union type is assigned, it can infer a type 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

// Property 'length' does not exist on type 'number'.
Copy the code

In the example above, myFavoriteNumber is inferred to string, and accessing its length property is not an error. MyFavoriteNumber is then inferred to number, and an error is reported when accessing its length property.

The type of the object — interface

In TypeScript, we use interfaces to define object types.

What is an interface

An interface is an important concept in object-oriented languages. It is an abstraction of behavior that needs to be implemented by classes. Let’s look at a simple example:

interface Person{
    name: string;
    age: number;
}

let tom: Person = {
    name: 'Tom'.age: 25
};
Copy the code

In the example above, we define an interface Person, followed by a variable Tom, whose type is Person. So we’re constrained that the shape of Tom must be the same as the interface Person. Interfaces generally start with uppercase letters. It is not allowed to define variables with more or less attributes than the interface.

Optional attribute

When we don’t want to match a shape exactly, we can use optional properties.

interface Person {
    name: string; age? :number;
}

let tom: Person = {
    name: 'Tom'
};
Copy the code

The implication of an optional attribute is that the attribute may not exist, which is still not allowed to add undefined attributes.

Any attribute

When we want an interface to allow arbitrary attributes, we can do this:

interface Person{
    name: string; age? :number;
    [propName: string] :any;
}
let tom: Person ={
    name: 'Tom'.gender: 'male'
};
Copy the code

The above example sets an optional attribute age and uses [propName: String] to define any attribute that takes a value of type string. There is a point to note here: once any attribute is defined, the types of both the determined attribute and the optional attribute must be subsets of its types, and an interface can define only one arbitrary attribute. If the interface has more than one type of attribute, you can use joint types in any attribute.

interface Person{
    name: string; age? :number;
    [propName:string] :string | number;
}

let tom: Person = {
    name: 'Tom'.age: 25.gender: 'male' 
};
Copy the code

Read-only property

When we want some fields in an object to be copied only at creation time, we can define read-only properties with readonly:

interface Person {
    readonly id: number;
    name: string; age? :number;
    [propName: string] :any;
}

let tom: Person = {
    id: 89757.name: 'Tom'.gender: 'male'
};

tom.id = 9527;
Copy the code

In the example above, the attribute ID defined by readonly was initialized and then assigned, so an error was reported. Note that the read-only constraint exists the first time an object is assigned, not the first time a read-only property is assigned.

TypeScript Introduction