advanced ts
ts-node XXX.ts
Execute the XXX.ts file
01: Advanced covariance (duck type) and inversion of ts
covariant
Subtypes can covariate with the main type (subtypes must all contain the main type key)
inverter
Subtypes can covariate with the main type (subtypes must all contain the main type key)
interface A {
name : string;
age : number;
}
interface B {
name : string;
age : number;
sex : string;
}
let a : A = {
name : "菜鸡" ,
age : 20 ,
} ;
let b : B = {
name : "小菜鸡" ,
age : 21 ,
sex : "男" ,
} ;
a = b;
let fnA = ( params : A ) => {
} ;
let fnB = ( params : B ) => {
} ;
fnB = fnA;
02: Advanced proxy and Reflect definition objects of ts
Proxy and Reflect definition objects
The Proxy object is used to create a proxy of an object, so as to realize the interception and customization of basic operations (such as attribute lookup, assignment, enumeration, function call, etc.)
02-1 Restrictions on proxy and Reflect types
type Person = {
name : string;
age : number;
text : string;
} ;
let proxy = ( obj : any, key : any) => {
return new Proxy ( obj, {
get ( target, prop, receiver) {
return Reflect. get ( target, prop, receiver) ;
} ,
set ( target, prop, value, receiver) {
return Reflect. set ( target, prop, value, receiver) ;
} ,
} ) ;
} ;
let logAccess = ( person: Person, key : "name" | "age" | "text" ) => {
return proxy ( person, key) ;
} ;
let p : Person = logAccess (
{
name : "ppp" ,
age : 20 ,
text : "我是菜鸡" ,
} ,
"name"
) ;
p. age = 100 ;
console. log ( "p" , p) ;
02-2 proxy and Reflect generics
type Person = {
name : string;
age : number;
text : string;
} ;
let proxy = ( obj : any, key : any) => {
return new Proxy ( obj, {
get ( target, prop, receiver) {
return Reflect. get ( target, prop, receiver) ;
} ,
set ( target, prop, value, receiver) {
return Reflect. set ( target, prop, value, receiver) ;
} ,
} ) ;
} ;
let logAccess = < T > ( person: T , key : keyof T ) : T => {
return proxy ( person, key) ;
} ;
let p2 = logAccess (
{
name : "ppp22" ,
text : "菜菜" ,
} ,
"name"
) ;
p2. text = "我就是说啊" ;
console. log ( "p2" , p2) ;
let p3 = logAccess (
{
name : "ppp22" ,
text : "菜菜" ,
id : 1 ,
} ,
"id"
) ;
03: Advanced Partial & Pick of ts
Partial sets the type defined by type, which can be reset to an optional type
Pick extracts certain key values on the type type to limit
type Person1 = {
name : string;
age : number;
sex : string;
} ;
type P1 = Partial< Person1> ;
let p1 : P1 = {
name : "ppp" ,
age : 10 ,
} ;
type Person2 = {
name : string;
age : number;
sex : string;
} ;
type p2 = Pick< Person2, "age" | "name" > ;
04: In the type, the generic type is passed in and each item is restricted ( readonly )
type R < T > = {
readonly [ P in keyof T ] : T [ P ] ;
} ;
type Person3 = {
name : string;
age : number;
text : string;
} ;
type p3 = R < Person3> ;
04: Advanced Record of ts to limit the key and value of the object
type P = {
[ key: string] : number;
} ;
let p : P = {
ppp : 10 , ooo : 10 } ;
type PP = Record< string, number> ;
let pp : PP = {
ppp : 10 ,
ooo : 10 ,
} ;
type PP2 = Record< string, string> ;
let pp2 : PP2 = {
ppp : "10" ,
ooo : "10" ,
} ;
interface term {
info : number;
checked : boolean;
}
type TermMap = Record< string, term> ;
let termMap : TermMap = {
xxx : {
info : 10 ,
checked : true ,
} ,
} ;
interface info_config {
name : string;
}
interface term2 {
info : info_config;
checked : boolean;
}
let termMap2 = {
} as Record< string, term2> ;
termMap2 = {
xxx : {
info : {
name : "111" ,
} ,
checked : true ,
} ,
} ;
interface info_config3 {
name : string[ ] ;
}
interface term3 {
info : info_config3;
checked : boolean;
}
let termMap3 = {
} as Record< string, term3> ;
termMap3 = {
xxx : {
info : {
name : [ "1" , "2" ] ,
} ,
checked : true ,
} ,
} ;