Typescript access dynamic property with [' '] syntax

后端 未结 1 442
Happy的楠姐
Happy的楠姐 2021-02-18 23:19
export class Foo{
 someproperty: string;
}

I am trying to understand why, when trying to access dynamic object property I can do the following as I saw

1条回答
  •  孤城傲影
    2021-02-19 00:02

    This is just a convention in TypeScript, available for convenience. If you want to access some arbitrary property that is not defined in the type signature of the object, you can use the ["foo"] notation, and the type checker will not try to enforce that the instance you're accessing has such a property in its type signature.

    0 讨论(0)
提交回复
热议问题