typescript 打字稿,类型对象不是通用的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37975346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Typescript, Type Object Is Not Generic
提问by Kris Hollenbeck
I am trying to create a type for an Object. But can't seem to get it right. This is what I have.
我正在尝试为对象创建类型。但似乎无法做到正确。这就是我所拥有的。
private test:Object<Test>;
this.test = {id : 'test'};
interface Test
{
id : string;
}
This doesn't work. This gives me the following error:
这不起作用。这给了我以下错误:
Type Object Is Not Generic
类型对象不是通用的
What is the right way (syntax) to create types for Objects like this?
为这样的对象创建类型的正确方法(语法)是什么?
采纳答案by Andrei Zhytkevich
Define a class Test
:
定义一个类Test
:
export class Test {
field1: number;
field2: string;
/// ...
}
then
然后
private test:Test;
Update: Sorry, didn't notice you have Test
as interface
. It's fine too.
更新:对不起,没注意你Test
的interface
。也没关系。
So same usage, you don't need Object<Test>
, just Test
所以同样的用法,你不需要Object<Test>
,只是Test