In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail the sample analysis of data types in Typescript. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
What is it
Typescript and javascript are almost the same, have the same data types, and provide more practical types for development based on javascript.
In the development phase, you can define a type for an explicit variable so that typescript can check the type at compile time, and an error will occur when the type does not match the expected results
What do you have?
The main data types of typescript are as follows:
1. Boolean (Boolean type)
2. Number (numeric type)
3. String (string type)
4. Array (array type)
5. Tuple (tuple type)
6. Enum (enumerated type)
7. Any (any type)
8. Null and undefined types
9. Void type
10. Never type
11. Object (object type)
Booleanlet flag:boolean = true;flag = 123; / / error flag = false; / / correct number
Numeric types, like javascript, typescript numeric types are floating-point numbers that support binary, octal, decimal, and hexadecimal.
Let num:number = 123 ternum = '456 percent; / / error num = 456; / / correct
Binary representation:
Let decLiteral:number = 6; / / decimal let hexLiteral:number = 0xFood; / / hexadecimal let binaryLiteral:number = 0b1010; / / binary let octalLiteral:number = 0o744; / / octal string
String types, like javascript, can be represented by double quotation marks (") or single quotation marks (')
Let str:string = 'this is ts';str =' test'
As a superset, you can also wrap it with a template string and embed variables through ${}
Let name:string = `Gene`; let sentence:string = `Hello, my name is ${name} `array
The array type, which is the same as javascript, is wrapped through [] and can be written in two ways:
Method 1: element type followed by []
Let arr:string [] = ['1213]; arr = [' 45]
Method 2: use array generics, Array:
Let arr:Array = [1jue 2]; arr = ['45th June 55']; tuple
Tuple type, which allows you to represent an array of known elements and types, and each element does not have to be of the same type
Let tupleArr: [number,string,boolean]; tupleArr = [12recovery34century true]; / / yestupleArr = [12recovery34']; / / noenum
The enum type is a supplement to the javascript standard data type. Enumerated types can be used to give a friendly name to a set of values.
Enum Color {Red,Green,Blue} let c:Color = Color.Green;any
You can specify values of any type. In the programming phase, it is not clear that variables of the type specify a type. You do not want the type checker to check these values but directly let them pass the compile phase check. In this case, you can use the any type.
Using the any type allows you to be assigned to any type, and you can even call its properties and methods
Let num:any = 123 ternum = 'str';num = true
When you define an array that stores various types of data, the sample code is as follows:
Let arrayList:any [] = [1 undefined false]]; arrayList [1] = 100 nulls and undefined
In javascript, null means "nothing", is a special type with only one value, represents an empty object reference, and undefined represents a variable with no set value.
By default, null and undefined are subtypes of all types, which means you can assign null and undefined to variables of type number
Let num:number | undefined; / / numeric type or undefinedconsole.log (num); / / correct num = 123; console.log (num); / / correct
But ts is configured with strictNullChecks tags, and null and undefined can only be assigned to void and each of them.
Void
Used to identify the type of the method's return value, indicating that the method has no return value.
Function hello (): void {alert ('Hello Runoob')} never
Never is a subtype of other types, including null and undefined, and can be assigned to any type, representing a value that never appears
But no type is a subtype of never, which means that variables of life never can only be assigned by the never type.
The never type is generally used to specify which exceptions are always thrown, an infinite loop
Let anever functions must have an unreachable destination function error (message:string): never {thorw new Error (message);} object
Object type, non-primitive type, common form wrapped by {}
Let obj:object;obj = {name:'Zhang',age:23}; this is the end of the article on "sample Analysis of data types in Typescript". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.