In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge about the basic types of TypeScript. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
TypeScript:
A language built on JavaScript
A superset of JavaScript
Can be executed on any platform that supports JavaScript
TypeScript extends JavaScript and adds types
TS cannot be executed directly by JS parsers, and TS is compiled into any version of JS [TS is fully compatible with JS]
What has been added to TypeScript?
Type (ts adds a type to a variable, and the type of js is a value)
Support for new features of ES
Added new features (interfaces, abstract classes) that ES does not have
Rich configuration options
Powerful development tools
The Construction of TypeScript Development Environment
Download Node.js
Install Node.js
Global installation of typescript using npm
Npm I-g typescript
Use tsc to compile ts files
Create a ts file
Proceed to the directory where the ts file is located
Tsc xxx.ts
Basic types of TypeScript
one. Type declaration
Type declaration allows you to specify the type of variables (parameters, formal parameters) in TS
After specifying a type, when assigning a value to a variable, the TS compiler automatically checks whether the value conforms to the type declaration, and if it does, it will be assigned, otherwise an error will be reported.
The type declaration sets the type so that variables can only store values of a certain type.
Syntax:
Let variables: types of
Let variable: type = value
Function fn (parameter: type, parameter: type): type {
...
}
two. Automatic type judgment
TS has an automatic type determination mechanism.
When a variable is declared and assigned at the same time, the TS compiler automatically determines the type of the variable.
When variables are declared and assigned at the same time, type declarations can be ignored
three. Types
Type example description
Number 1,-1, 1.5 any number
String 'hi', "hi", hi any string
Boolean true, false Boolean value true or false
The value of the literal quantity itself is the value of the literal quantity.
Any * any type
Unknown * Type safe any
Void null (undefined) has no value (or undefined)
Never has no value and cannot be any value
Object {name:'jiangjiang'} arbitrary JS object
Array [1, 2, 3] arbitrary JS array
Tuple [4jue 5] element, TS new type, fixed length array
Enum enum {A, B} enumeration, new types in TS
-number
[all numbers in TS are floating-point numbers with type number]
Support: binary, octal, decimal, hexadecimal literals
Let binary: number = 0b1010
Let octal: number = 0o744
Let decimal: number = 6
Let hex: number = 0xf00d
-string
Support: single quotation marks, double quotation marks, template string
Let color: string = "blue"
Color = 'red'
Let fullName: string =-- Bob Bobbington--
Let age: number = 18
Let sentence: string =-- Hello, my name is ${fullName}.
I'll be ${age + 1} years old next month.--
-boolean
Let isDone: boolean = false
-literal quantity
[the literal quantity is used to specify the type of the variable, and the range of the variable can be determined by the literal quantity]
Let color: 'red' |' blue' | 'black'
Let num: 1 | 2 | 3 | 4 | 5
-any
-any represents any type. Setting the type of a variable to any is equivalent to turning off TS type detection for that variable.
-declare a variable if you do not specify a type, the TS parser automatically determines that the variable is of type any (there is an implicit ang)
Let d: any = 4
D = 'hello'
D = true
-when assigning an any type to a string, no error is reported, and s also turns off TS type checking; d is of type any, which can be assigned to any variable
Let s:string
S = d
-unknown
-unknown is actually a type-safe variable of type any;unknown, which cannot be directly assigned to other variables.
Let e:unknown
EBay 10
/ / Type "unknown" cannot be assigned to type "string", an error will be reported.
/ / s = e
If (typeof estrangement fighting) {
S = e
}
-void
[indicates null, taking a function as an example, indicates a function that does not return a value]
Function fn (): void {
/ / return
/ / return undefined
/ / return null
}
-never
[indicates that the result will never be returned (not even null,undefined), which is used to return error messages and interrupt the program]
Function fn2 (): never {
Throw new Error ("wrong report")
}
-object
[represents a js object]
Let a:object
A = {}
A = function () {}
{} is used to specify which attributes can be contained in an object
Syntax: {attribute name: attribute value, attribute name: attribute value}
Add? after the attribute name? Indicating that the property is optional
Let b: {name:string,age?:number}
B = {name:'jianjiang'}
If you want to add multiple attribute pairs of uncertain types, you can use [propName:string]: any to represent any type of attribute:
Let c: {name:string, [propName:string]: any}
C = {name: "jiangjiang", age:18,gender:' female'}
Set the type declaration of the function structure:
Syntax: (parameter: type, parameter: type) = > return value
Let d: (a number) = > number
D=function (N1 and N2) {
Return n1+n2
}
D (1)
-array
Type declaration of the array:
Type []
Array
/ / string [] represents an array of strings
Let e:string []
E = ['axiajiaoyuzhongyuanshou, bangzhongyuanshou]
/ / number [] represents a numeric array
Let f:number []
Let g:Array
G = [1pm 2pm 3]
-tuple
Meta-ancestor: a fixed-length array (unlike an array, each element of the array is of the same type, while the tuple allows each element to be of a different type)
Syntax: [type, type]
Let h: [string,number]
H = ['hello',123]
-enum [enumeration]
Enum Color {
Red
Green
Blue
}
Let c: Color = Color.Green
Enum Color {
Red = 1
Green
Blue
}
Let c: Color = Color.Green
Enum Color {
Red = 1
Green = 2
Blue = 4
}
Let c: Color = Color.Green
-type [aliases of types: simplify the use of types]
Type myType = 1 | 2 | 3 | 4 | 5
Let k:myType
Kenz2
-Type assertion
-in some cases, the type of a variable is clear to us, but not to the TS compiler. At this point, you can tell the compiler the type of a variable through type assertions, which can take two forms:
First kind
Let someValue: unknown = "this is a string"
Let strLength: number = (someValue as string). Length
The second kind
Let someValue: unknown = "this is a string"
These are all the contents of the article "what are the basic types of TypeScript". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.