在TypeScript中声明动态添加的类属性 [英] Declare dynamically added class properties in TypeScript

查看:1505
本文介绍了在TypeScript中声明动态添加的类属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将属性分配给类的实例,而无需了解TypeScript中的属性名称,值和值的类型。假设我们有以下 example.ts 脚本:

I want to assign properties to the instance of a class without knowing the property names, values and types of values in TypeScript. Lets assume we have the following example.ts script:

// This could be a server response and could look totally diffent another time...
const someJson:string = '{ "foo": "bar", "bar": "baz" }'

class MyClass {
  someProperty:boolean

  constructor( json:string ) {
    const parsedJson:any = JSON.parse( json )

    Object.keys( parsedJson ).forEach(
      ( key:string ) => {
        this[ key ] = parsedJson[ key ]
      }
    )

    this['someProperty'] = true
  }
}

const myInstance = new MyClass( someJson )

// Works fine, logs `true`.
console.log( myInstance.someProperty )

// Error: Property 'foo' does not exist on type 'MyClass'.
console.log( myInstance.foo )

// Error: Property 'bar' does not exist on type 'MyClass'.
console.log( myInstance.bar )

如何确保TypeScript编译器不会抱怨动态添加的属性,而是将它们作为 key:任何类型的值对来处理。我仍然希望 tsc 确保 myInstance.someProperty 的类型必须为 boolean ,但我希望能够获得 myInstance.what ,即使未定义它也不会遇到编译器错误。

How can I make sure that the TypeScript compiler does not complain of the dynamically added properties but instead handle them as "key": value pairs of any type. I still want tsc to make sure that myInstance.someProperty has to be of type boolean but I want to be able to get myInstance.whatever even if it is not defined without running into compiler errors.

我找不到任何使我明白的文档。可能是因为我不是说英语的人。因此,请保持答案简单。

I did not find any documentation that makes this clear to me. Maybe because I'm not a native english speaker. So please keep the answers simple.

编辑:

我记得类似于以下内容,但我从来没有做到过:

I remember that there was something like the following but I never got that to work:

interface IMyClass {
  [name:string]: any
}


推荐答案

问题是在运行时添加新属性,编译器无法得知。

The problem is that you're adding the new properties at runtime and the compiler has no way of knowing that.

如果您事先知道属性名称,则可以执行以下操作:

If you know the property names in advance then you can do this:

type Json = {
    foo: string;
    bar: string;
}

...

const myInstance = new MyClass(someJson) as MyClass & Json;
console.log(myInstance.foo) // no error






编辑



如果您不提前知道属性,则不能执行以下操作:


Edit

If you do not know the properties in advance then you can't do this:

console.log(myInstance.foo);

因为然后您知道 foo 是其中的一部分收到的json中,您可能会有类似的内容:

Because then you know that foo is part of the received json, you'll probably have something like:

let key = getKeySomehow();
console.log(myInstance[key]);

这应该在编译器没有错误的情况下起作用,唯一的问题是编译器没有不知道返回值的类型,它将是 any

And this should work without an error from the compiler, the only problem with that is that the compiler doesn't know the type for the returned value, and it will be any.

因此,您可以执行以下操作:

So you can do this:

const myInstance = new MyClass(someJson) as MyClass & { [key: string]: string };
let foo = myInstance["foo"]; // type of foo is string
let someProperty = myInstance["someProperty"]; // type of someProperty is boolean






第二次编辑



您确实知道道具,但在课堂上不知道,您可以这样做:


2nd edit

As you do know the props, but not in the class, you can do:

type ExtendedProperties<T> = { [P in keyof T]: T[P] };
function MyClassFactory<T>(json: string): MyClass & ExtendedProperties<T> {
    return new MyClass(json) as MyClass & ExtendedProperties<T>;
}

然后,您只需像这样使用它即可:

Then you simply use it like so:

type Json = {
    foo: string;
    bar: string;
};
const myInstance = MyClassFactory<Json>(someJson);

请注意,这仅适用于打字稿2.1及更高版本。

Note that this will work only on typescript 2.1 and above.

这篇关于在TypeScript中声明动态添加的类属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆