TypeScript-类型交叉点&;这个关键词
以下两者之间的区别是什么:TypeScript-类型交叉点&;这个关键词,types,typescript,Types,Typescript,以下两者之间的区别是什么: interface MyType { f<T>(other: T): this & T; } 接口MyType{ f(其他:T):本&T; } 及 接口MyType{ f(其他:T):MyType&T; } ?此类型取决于实现类,因此给出第一个定义后,将进行以下类型检查: class Foo implements MyType { f<T>(other: T): T & this { ... } g
interface MyType {
f<T>(other: T): this & T;
}
接口MyType{
f(其他:T):本&T;
}
及
接口MyType{
f(其他:T):MyType&T;
}
?此
类型取决于实现类,因此给出第一个定义后,将进行以下类型检查:
class Foo implements MyType {
f<T>(other: T): T & this { ... }
g(): string { return "only in foo"; }
}
var foo: Foo
var ff = foo.f("dsklf");
var s: string = ff.g();
类Foo实现MyType{
f(其他:T):T&这个{…}
g():字符串{return“仅在foo中”;}
}
变量foo:foo
var ff=foo.f(“dsklf”);
var s:string=ff.g();
由于ff
具有类型Foo&string
,因此是Foo
的子类型
鉴于MyType
的第二个定义,ff.g()
将不会进行类型检查,因为g
未在MyType&string
上定义
class Foo implements MyType {
f<T>(other: T): T & this { ... }
g(): string { return "only in foo"; }
}
var foo: Foo
var ff = foo.f("dsklf");
var s: string = ff.g();