当前位置:网站首页>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 :
undefined
andnull
It 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
any
type : I'm not sure what type of current data is, and I want to store it , have access to anyvoid
type : Function has no return value
function show():void{
}
object
type- 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]{
}
边栏推荐
- Thermal resistance PT100 cu50 isolation converter to 4-20mA analog output temperature transmitter 0-10V
- busybox date 日期增加一天明天 网上都是减一天 昨天
- 【力扣】二叉树的最大深度
- 你的企业最适合哪种深度学习?
- 2022/07/12 学习笔记 (day05)JS内置函数
- busybox 指定日期修改 暂时不需要clock -w 写入硬件
- Fs4061a (5V USB input, double lithium battery series application, 5V boost charging, 8.4v Management IC
- js变量提升
- 【力扣】相同的树
- 5V boost charging 8.4v chip
猜你喜欢
2022/07/12 学习笔记 (day05)循环
Introduction to Darwin streaming server
简单Chrome脚本 自动跳过b站视频播放结束后的的充电鸣谢页面
It4058a single lithium ion battery charging management
面试复习第N次
2022/07/14 学习笔记 (day07)数组
ES聚合分析报错:“reason“ : “Text fields are not optimised for operations
Qt Creator闪退解决办法
Hm9922 switching buck LED constant current driver IC
Thermal resistance PT100 cu50 isolation converter to 4-20mA analog output temperature transmitter 0-10V
随机推荐
【力扣】用栈实现队列
[usaco06dec]the fewest coins g (hybrid backpack)
获取当前年月日、时分秒、星期,并实时更新
busybox date 时间的加减
uboot 编译前的配置命令make config分析
第五章 我们可以直接调用run()方法来启动一个新线程吗
MCU single chip OTP
RestAPI实现聚合(黑马教程)
开源在线的MarkDown编辑器 --【Editor.md】
RestClient查询文档
2021 - 09 - 15
Ehab the xorcist (XOR property, construction)
HRA 1~12w series 12V wide voltage 9~18v to ± 250VDC boost converter
數學基礎課2_歐拉函數,線性篩,擴歐
解决:无法加载文件 C:\Program Files\.. 因为在此系统上禁止运行脚本...
Configure the 'log' shortcut key in vscode and remove the console log(‘‘); Semicolon in;
Cable TV network (tree grouping)
计算几何(2)
2021-11-17 esp32 pin reference
Acwing第57场周赛(AK)