Home  >  Article  >  Web Front-end  >  What is typescript? Introduction to typescript basic types

What is typescript? Introduction to typescript basic types

不言
不言forward
2018-10-19 14:33:443308browse

The content of this article is about what typescript is? The introduction of the basic types of typescript has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Overview

Javascript is a weakly typed language. Weak typing is very arbitrary and flexible. This is its advantage and also its disadvantage; the same variable , it can be either a numeric type, a string type, or various other object types. In js, if you are given a variable name, can you determine its type at a glance? I am afraid that in most cases, you Not sure, even if a number is assigned to it at the beginning, how do you know that the intermediate code will not change it into other types.

Strong type and weak type

First of all, let us briefly distinguish between strong type and weak type language. Of course, js is a veritable weak type language

Strong type

Given a variable, you must first declare its type int a; Of course, you can also assign an initial value to it at the same time, int a = 1;. If we assign a value to it later, we can only assign it to an integer type, a = 2;. If you want to assign it a string type, such as a = "xxx";, the compiler will directly report an error to you, so let's talk about it. There is no follow-up process.

Weak type

Weakly typed languages ​​do not have these restrictions

Let’s get to the point

typescript

Typescript is a superset of javascript. It is fully compatible with javascript, but it also extends many functions. I believe you will fall in love with it after understanding it. Why do you say this? Because given you an object, you can use The editor can only prompt you to see what properties, methods, etc. are in it, without having to look for definitions everywhere. The official website of typescript is here

The form of declaring variable type is var a: Type, Type is the type. Once the Type type is declared, subsequent a can only receive the Type type and cannot receive other types, because the editor will The smart reminder you give will of course give you an error when compiling.

Basic types

Basic types include number, string, boolean, undefined, null

var v1: number = 1
var v2: string = 'hello'
var v3: boolean = true
var v4: undefined = undefined
var v5: null = null

Array type

// 字串数组
var arr_s: string[] = ['xxx', 'yyy']
// 数字数组
var arr_n: number[] = [1, 2]

Enumeration

enum Gendar {
    male,
    female
}
var g: Gendar = Gendar.male

Universal type

any is equivalent to not declaring any type

var a: any = 123

The above is the detailed content of What is typescript? Introduction to typescript basic types. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete