当前位置:网站首页>Typescript learning
Typescript learning
2022-07-19 06:11:00 【Culvert.】
Automatic compilation ts File for js:tsc --init
Compile in real time :tsc -w ./path/file.ts
Type notes : One is function or Variable Added constraint
Interface : Use interface Define an object , It defines the data format
TS Basic data type :
undefinedandnullIt can be used as a subtype of other types- To define an array :
let arr1:number[]=[1,2,3]
let arr2:Array<number>=[1,2,3]
- Enumeration type :
enum
enum Color={
red,
blue
}
let color:Color=Color.red // The output is the number of the element , namely 0
anytype : I'm not sure what type of current data is, and I want to store it , have access to anyvoidtype : Function has no return value
function show():void{
}
objecttype- Joint type :
function show(str:number|string){
}
- Types of assertions :
// If you don't know whether the incoming data is a number or a string , And want to get its length
if((<string>str).length){
return (<string>str).length
}
Interface
interface IPerson{
readonly name:string, // Indicates that the property is read-only
age?:number //? Express age It's OK to have this attribute without
}
Function type
interface IPerson{
(name:string,age:number):boolean
}
const person:IPerson=function(name:string,age:number):boolean{
}
Class types
interface IPerson1{
fly1()
}
interface IPerson2{
fly2()
}
// Define an interface to inherit multiple interfaces
interface IPerson extends IPerson1,IPerson1{
}
// Implement the methods in the interface , Class can implement multiple interfaces
class Person implements IPerson1,IPerson2{
fly1(){
console.log('1')
}
fly2(){
console.log('2')
}
}
polymorphic
class Animal{
}
class Dog extends Animal{
}
class Pig extends Animal{
}
const dog:Animal=new Dog()
const pig:Animal=new Pig()
Modifier ( It mainly describes the accessibility of members in a class )
public // All accessible
private // Class cannot be accessed outside , Subclasses are not accessible
protected // Class cannot be accessed outside , Subclasses can be accessed internally
readonly Decorate the member attributes in the class , Can be in constructor Modify this attribute in
readonly Decorate the parameters in the constructor in the class , Class will contain this attribute member
class Person{
constuctor(readonly name:string){
this.name=name
}
}
const p=new Person('x')
console.log(p.name) // x
Accessors (get and set)
Static attribute (static)
Can't be in constructor Use in this call static attribute , Only classes can be used . Method of attribute
class Person{
static name1:string
static show(){
console.log(1)
}
}
const person=new Person('x')
console.log(Person.name,Person.show()) // Note that Person class , Instead of instance objects person
abstract class ( Contains abstract methods , But the method has no specific implementation )
abstract class Animal{
abstract eat(){
}
}
class Dog extends Animal{
eat(){
console.log(1)
}
}
Function and function type
cosnt add:(x:number)=>number=function(x:number):number{
}
The remaining parameters
function show(str:string,...args:string[]){
}
function overloading
function add(x:string,y:string):string
function add(x:number,y:number):number
function add(x:number|string,y:number|string):number|string{
}
Generic
Defining functions in 、 Interface 、 Class time , The data type to be used cannot be predetermined
function add<T>(value:T):T[]{
}
const arr=add<number>(200)
// Multiple generics
function add<T,K>(value:T,num:K):[T,K]{
}
边栏推荐
- Guess the string (dichotomy, interaction)
- Hm9922 switching buck LED constant current driver IC
- 解决:无法加载文件 C:\Program Files\.. 因为在此系统上禁止运行脚本...
- 开源在线的MarkDown编辑器 --【Editor.md】
- 4-channel encoder pulse counter, speed measurement, 8-Channel do, Modbus TCP data acquisition module
- Where have all the older programmers gone?
- Busybox date date increases by one day, and decreases by one day on the Internet tomorrow
- XOR-gun (位运算,思维,区间暴力)
- 【牛客】二叉树遍历
- It4058a single lithium ion battery charging management
猜你喜欢

Antd is not defined

索引库中的文档的操作

【力扣】翻转二叉树

解决:无法加载文件 C:\Program Files\.. 因为在此系统上禁止运行脚本...

2021-11-17 esp32 pin reference

Wireless charging mouse pad RGB LED lighting wireless charging mouse pad

RestAPI实现聚合(黑马教程)

Basic mathematics course 2_ Euler function, linear sieve, extended Euler
![MySQL workbench basically uses [create a database]](/img/d2/a753e9c77bb17aaff1d0e9dc11c803.png)
MySQL workbench basically uses [create a database]

Complete scheme diagram of lth7 five pin chip fs4054 charging circuit principle
随机推荐
Basic mathematics course 2_ Euler function, linear sieve, extended Euler
5V boost charging 8.4v chip
Lithium battery charging management chip
【力扣】单值二叉树
数学基础课2_欧拉函数,线性筛,扩欧
Guess the string (dichotomy, interaction)
It4058a single lithium ion battery charging management
2021-11-17 esp32 pin reference
Wide voltage input high voltage output voltage control type
Hm9922 switching buck LED constant current driver IC
QT creator flashback solution
make menuconfig缺少ncurses
Qtss callback routine
Solve cannot read properties of null (reading 'pickalgorithm')
【力扣】相同的树
Make config analysis of configuration commands before uboot compilation
XOR gun (bit operation, thinking, interval violence)
Qtss data type
【力扣】对称二叉树
串口循环缓存区 简单 免初始化 不用堆、指针、分段memcpy