1. Differences between TS and JS
TS is a language based on JS, developed by Microsoft, which has become increasingly important in the front-end field. In my personal understanding, the essential difference between TS and JS is that TS is a strongly typed language, while JS is a weakly typed language. (There is no specific definition for strongly typed and weakly typed languages. In my opinion, the difference between TS and weakly typed languages is that there is no invisible conversion, and the type of variables cannot be changed at will.)
2. New type definition of TS based on JS
(1) Tuple type
Tuples are arrays, but in TS tuples are arrays that can hold different types
const mytuple = [10."Hello"."World"."typeScript"];
Copy the code
(2) Enum type (enum)
Enumeration types are used to define collections of values.
enum Jay {
jay = 3,
jin = 1,
xiaoJAY = 2
}
Copy the code
0,1,2 are values for keys like jay, and if you don’t add a number then it increments from 0
enum Jay {
jay, / / 0
jin, / / 1
xiaoJAY / / 2
}
Copy the code
Enumerations are often used when a project declares constants
(3) Void type (void)
Used to identify the type of value returned by a function method, indicating that the method has no return value.
function hello() :void {
alert("Hello Runoob");
}
Copy the code
(4) Never type (never)
Never is a subtype of other types, including null and undefined, and represents a value that never occurs.
let x: never;
let y: number;
// Error running. The numeric type cannot be converted to never
x = 123;
// The never type can be assigned to the never type
x = (() = >{ throw new Error('exception')}) ();// It works correctly. The never type can be assigned to a numeric type
y = (() = >{ throw new Error('exception')}) ();// A function that returns never can be the case where an exception is thrown
function error(message: string) :never {
throw new Error(message);
}
// A function that returns never can be a terminating point that cannot be executed
function loop() :never {
while (true) {}}Copy the code
3. Other definitions in TS
(1) interface (interface)
An interface is a set of abstract method declarations, a collection of method characteristics that should be abstract and implemented by a concrete class that can then be invoked by a third party to make the concrete class execute the concrete method.
interface Person {
firstName:string.lastName:string, allName ? :string / /? It means that the modified attribute is optional during inheritance
sayHi: () = >string
}
// The object that inherits the interface must have all the necessary data in the interface, otherwise an error will be reported
const customer:Person = {
firstName:"Tom".lastName:"Hanks".sayHi: (a) :string= >{return "Hi there"}}interface namelist {
[index:string] :string // Written this way to indicate that this can accept any string of arguments
}
/ / the extends keyword
interface Person {
age:number
}
interface Musician extends Person {
instrument:string
}
interface Person1 {
age:number
}
interface Musician extends Person {
instrument:string
}
// The age and instrument attributes must be present at the same time
let drummer: Musician = {
age: 10.instrument: 'jay'
};
Copy the code
(2) Namespace (namespace)
One of the most explicit purposes of namespaces is to solve the duplication problem.
A namespace defines the visible scope of an identifier. An identifier can be defined in multiple namespaces, and its meaning in different namespaces is irrelevant. This way, any identifiers can be defined in a new namespace, and they will not conflict with any existing identifiers, because the existing definitions are in other namespaces.
namespace SomeNameSpaceName {
export interface ISomeInterfaceName { }
export class SomeClassName {}}Copy the code
To call from another namespace, the syntax is:
SomeNameSpaceName.SomeClassName;
Copy the code