The base types are my own, and the main types introduced in this chapter are common in Javascript. In addition, it is also different from the advanced types in the next chapter (data types that Javascript does not have).
Imagine the basics
There are two types of JavaScript:
- Primitive Data Types
- Object Types
The primitive data types include: Boolean, number, string, NULL, undefined, and the new type Symbol in ES6. This chapter covers the first five options and null values.
Boolean value
It is the most basic data type and has a value of true/false. Boolean is defined in both JavaScript and TypeScript (and in other languages as well).
// boolean.ts
let isDone: boolean = false;
Copy the code
After compiling (TSC Boolea.ts)
// boolean.js
var isDone = false;
Copy the code
Q: Can I use constructor Boolean to create? Such as
// boolean2.ts
let isDoneByNewBoolean: boolean = new Boolean(1);
Copy the code
Before compiling (in editor)
The compiled
The problem is that new Boolean(1) returns an object.
How about using Boolean(1) directly?
// boolean3.ts
let isDoneByBoolean: boolean = Boolean(1);
Copy the code
The compiled
// boolean3.js
var isDoneByBoolean = Boolean(1);
Copy the code
Boolean(1) returns a Boolean value.
digital
Like JavaScript, all numbers in TypeScript are floating point numbers of type number.
// number.ts
let decLiteral: number = 6;
let hexLiteral: number = 0xf00d;
let binaryLiteral: number = 0b1010; // Binary representation in ES6
let octalLiteral: number = 0o744; // Octal notation in ES6
let notANumber: number = NaN;
let infinityNumber: number = Infinity;
Copy the code
The compiled
// number.ts
var decLiteral = 6;
var hexLiteral = 0xf00d;
var binaryLiteral = 10; // Binary representation in ES6
var octalLiteral = 484; // Octal notation in ES6
var notANumber = NaN;
var infinityNumber = Infinity;
Copy the code
string
Like JavaScript, strings are used to represent text data types, and you can use ** double quotes (“) or single quotes (‘) ** to represent strings.
// string.ts
let str: string = 'Typescript';
let say: string = `Hello, ${str}`;
Copy the code
The compiled
// string.js
var str = 'Typescript';
var say = "Hello, " + str;
Copy the code
Null, and undefined
In the TypeScript world, null and undefined define data types themselves. But it is of little use.
// null-undefined.ts
let u: undefined = undefined;
let n: null = null;
Copy the code
The compiled
// null-undefined.js
var u = undefined;
var n = null;
Copy the code
Q: What about swapping null and undefined?
// null-undefined2.ts
let u: undefined = null;
let n: null = undefined;
Copy the code
The compiled
// null-undefined2.js
var u = null;
var n = undefined;
Copy the code
Q: How about something like this?
// null-undefined3.ts
let nu: number = undefined;
let nn: number = null;
let su: string = undefined;
let sn: string = null;
let bu: boolean = undefined;
let bn: boolean = null;
Copy the code
The compiled
// null-undefined3.js
var nu = undefined;
var nn = null;
var su = undefined;
var sn = null;
var bu = undefined;
var bn = null;
Copy the code
From the above three examples, null and undefined are subtypes of all types.
A null value
In the JavaScript world there is no such thing as a null value, but in TypeScript you can use void to represent functions that do not return any value.
// void.ts
function sayTs() :void {
console.log('Hello, Typescript');
}
function sayTs2() :void {
return 'Hello, Typescript';
}
Copy the code
Before compiling
The compiled
But it was compiled
// void.js
function sayTs() {
console.log('Hello, Typescript');
}
function sayTs2() {
return 'Hello, Typescript2';
}
Copy the code
Q: What is the relation between void and null (or undefined)?
// void2.ts
let u: void = undefined;
let n: void = null;
Copy the code
The compiled
// void2.js
var u = undefined;
var n = null;
Copy the code
Q: How does void relate to string(number, Boolean)?
// void3.ts
let n: void = 1;
let b: void = false;
let s: void = '1';
Copy the code
Before compiling
The compiled
But it was compiled
// void3.js
var vn = 1;
var vb = false;
var vs = '1';
Copy the code
To summarize, only undefined, null, and functions with no return value can be assigned to void.
This code Github
You can…
Previous: Typescript’s growth environment
Next: Typescript advanced types
Contents: A primer to Typescript’s short book