TypeScript:任何普通对象的泛型
编辑以下是我解释问题的实际代码: 使用TypeScript,我想指定一个泛型,它是普通对象的任何数据结构TypeScript:任何普通对象的泛型,typescript,typescript-typings,Typescript,Typescript Typings,编辑以下是我解释问题的实际代码: 使用TypeScript,我想指定一个泛型,它是普通对象的任何数据结构 class MyClass<T extends {}> { public vars: T; constructor() { this.vars = {}; } } 这个错误完全合乎逻辑 使用MyType的第一个示例,这是等效的MyClass,没有泛型: 接口MyType{ foo:string; 条:数字; } 类MyClass{ 公共变量:MyType
class MyClass<T extends {}> {
public vars: T;
constructor() {
this.vars = {};
}
}
这个错误完全合乎逻辑 使用
MyType
的第一个示例,这是等效的MyClass
,没有泛型:
接口MyType{
foo:string;
条:数字;
}
类MyClass{
公共变量:MyType;
构造函数(){
this.vars={};
}
}
由于
vars
现在必须具有foo
和bar
成员,因此不允许分配vars={}
。将vars
的类型定义为部分
这里的问题是,您已经将变量定义为partial。所以你永远不会知道它是否真的充满了价值
您可以使用运行时类型验证程序来检查:
export function isMyType(value: any): value is MyType {
return typeof value['foot'] === 'string'
&& typeof value['bar'] === 'number';
}
const x: Partial<MyType> = {};
if(isMyType(x)) {
const y: MyType = x; // there is no TS, because of the isMyType check
}
导出函数isMyType(值:any):值为MyType{
返回值的类型['foot']='string'
&&值的类型['bar']='number';
}
常数x:Partial={};
if(isMyType(x)){
const y:MyType=x;//由于isMyType检查,没有TS
}
我忘记了TypeScript中的是什么
操作符被调用了,但是当在条件块中使用时,选中变量的类型会改变。所以TypeScript不会对此抱怨
如果值不是您认为应该的值,它还允许您更改以抛出运行时错误。我认为这可能会解决此问题
class MyClass<T extends {[key:string]: any}> {
public vars: T;
constructor() {
this.vars = {};
}
}
class-MyClass{
公共变量:T;
构造函数(){
this.vars={};
}
}
例如:
接口MyType{
foo:string;
条:数字;
}
let test=new MyClass();
在test中,您将能够找到MyType类型的变量,在test.vars中,您可以看到foo和bar属性可用T扩展了{},但{}并不一定扩展了T。因此,将{}分配给T类型的变量确实是不正确的。假设您创建了一个扩展MyClass的类MyClassOfCar。{}不是汽车,是吗?JB回答说,你不能这样做,因为它没有逻辑意义。看一下您的实际代码,我认为使用类型参数对于基本解析器类来说没有什么意义。它不会以任何方式强制执行该类型,它还可以返回any
,更高级别的类可以强制执行该类型。使用MyType直接违背了目的。@Lukas我的示例旨在说明编译器生成的中间阶段,我并不建议您将MyType
硬编码到MyClass
。但是我不得不问,你在这里用泛型实现了什么,而仅仅用any
是无法实现的?我想通过调用泛型wait类来定义方法的返回值,我将把真实的示例添加为gis,你可以看到我的真实示例:嘿,谢谢,我尝试了这个,这个仍然有问题。请参阅我的类,它使用泛型is called解析器
作为返回变量的方法,并使用:Partial
。现在我这样调用它:parse(data:DataView):ParseResult{returnnewparser(data).char(“state”).char(“substate”).vars();}
我得到错误属性“state”在类型“Partial”中是可选的,但在类型“ParseResult”中是必需的。
state
在ParseResult
@Lukas中是必需的字符串
是的,我应该添加一个警告。一旦类型被标记为分部,它就始终是分部的。不能将其分配给非部分版本。TypeScript无法知道运行时是否具有所有属性。我将更新我的答案,但使用partials可能会很棘手。请参见以下示例:
class MyClass<T extends {}> {
public vars: T;
constructor() {
this.vars = {};
// ^^^^
}
}
Type '{}' is not assignable to type 'T'.
'{}' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.ts(2322)
class MyClass<T> {
public vars: Partial<T>;
constructor() {
this.vars = {};
}
}
const x = new MyClass<MyType>();
console.log(x.vars.foo); // prints "undefined" but there is no TS error
console.log(x.vars.thing); // prints "undefined" but TS error for unknown property.
const x: Partial<FooBar> = {};
const y: FooBar = x; // TS error, because x is partial.
const y: FooBar = x as FooBar;
export function isMyType(value: any): value is MyType {
return typeof value['foot'] === 'string'
&& typeof value['bar'] === 'number';
}
const x: Partial<MyType> = {};
if(isMyType(x)) {
const y: MyType = x; // there is no TS, because of the isMyType check
}
class MyClass<T extends {[key:string]: any}> {
public vars: T;
constructor() {
this.vars = {};
}
}
interface MyType {
foo: string;
bar: number;
}
let test = new MyClass<MyType>();