Typescript basic types

Basics brain to make up

In JavaScript divided into two types:

  • Primitive data types (Primitive data types)
  • Object type (Object types)

Wherein, the original data type comprises: boolean, numeric, string, null, undefined and ES6 new type of the Symbol . There are five kinds null earlier in this chapter selective speak.

Boolean value

It is the most basic data type, its value is true/false. In JavaScript and TypeScript in use booleanis defined (in other languages, too).

// boolean.ts
let isDone: boolean = false;
复制代码

Compile ( tsc boolean.ts) after

// boolean.js
var isDone = false;
复制代码

Q: constructor Booleanto create can it? such as

// boolean2.ts
let isDoneByNewBoolean: boolean = new Boolean(1);
复制代码

Compiler (editor) ago

Compiled

The problem is that new Boolean(1)returns an object.

Ask: direct use of Boolean(1)what?

// boolean3.ts
let isDoneByBoolean: boolean = Boolean(1);
复制代码

Compiled

// boolean3.js
var isDoneByBoolean = Boolean(1);
复制代码

Boolean(1) It returns a Boolean value.

digital

And, like JavaScript, TypeScript where all numbers are floating point type number.

// number.ts
let decLiteral: number = 6;
let hexLiteral: number = 0xf00d;
let binaryLiteral: number = 0b1010; // ES6 中的二进制表示法
let octalLiteral: number = 0o744;   // ES6 中的八进制表示法
let notANumber: number = NaN;
let infinityNumber: number = Infinity;
复制代码

Compiled

// number.ts
var decLiteral = 6;
var hexLiteral = 0xf00d;
var binaryLiteral = 10; // ES6 中的二进制表示法
var octalLiteral = 484; // ES6 中的八进制表示法
var notANumber = NaN;
var infinityNumber = Infinity;
复制代码

String

And JavaScript, the use of stringtext data representing type, can be used ** double quotes ( ") or a single quote ( ') ** indicates string.

// string.ts
let str: string = 'Typescript';
let say: string = `Hello, ${str}`;
复制代码

Compiled

// string.js
var str = 'Typescript';
var say = "Hello, " + str;
复制代码

null 和 undefined

TypeScript in the world, nulland undefinedcan be used to define their own data types. But it is not books.

// null-undefined.ts
let u: undefined = undefined;
let n: null = null;
复制代码

Compiled

// null-undefined.js
var u = undefined;
var n = null;
复制代码

Q: That would value nulland undefinedwhat will happen to change the position it?

// null-undefined2.ts
let u: undefined = null;
let n: null = undefined;
复制代码

Compiled

// null-undefined2.js
var u = null;
var n = undefined;
复制代码

Q: So something like this happen?

// 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;
复制代码

Compiled

// null-undefined3.js
var nu = undefined;
var nn = null;
var su = undefined;
var sn = null;
var bu = undefined;
var bn = null;
复制代码

From the above three examples, it may be substantially determined, nulland undefinedall types of sub-type (can nulland undefinedassigned to numberall other types of variables, not much significance is assigned).

Null

In the JavaScript world is not null concept, but in TypeScript, you can use voidindicates that no function return values.

// void.ts
function sayTs(): void {
    console.log('Hello, Typescript');
}

function sayTs2(): void {
    return 'Hello, Typescript';
}
复制代码

Before compilation

Compiled

But still compiled out

// void.js
function sayTs() {
    console.log('Hello, Typescript');
}
function sayTs2() {
    return 'Hello, Typescript2';
}
复制代码

Q: voidwith null(or undefinedhow the relationship) between?

// void2.ts
let u: void = undefined;
let n: void = null;
复制代码

Compiled

// void2.js
var u = undefined;
var n = null;
复制代码

Q: voidand string( number, boolean) What is the relationship?

// void3.ts
let n: void = 1;
let b: void = false;
let s: void = '1';
复制代码

Before compilation

Compiled

But still compiled out

// void3.js
var vn = 1;
var vb = false;
var vs = '1';
复制代码

Under summary, can only undefined, nulland no return value of the function assigned to the void(null) type.

The Code Github

You can

Previous: Typescript growth environment

Next: Typescript any value

Reproduced in: https: //juejin.im/post/5d0b9e1f51882531fc431d60

Guess you like

Origin blog.csdn.net/weixin_33670786/article/details/93173970