【发布时间】:2017-12-06 12:52:09
【问题描述】:
如何在 typescript 中为类添加属性?
export class UserInfo {
public name:string;
public age:number;
}
let u:UserInfo = new UserInfo();
u.name = 'Jim';
u.age = 10;
u.address = 'London'; // Failed to compile. Property 'address' does not exist on type 'UserInfo'.
如何做到这一点?
【问题讨论】:
-
你想达到什么目的? typescript 的全部目的是拥有定义良好的接口和类,这样你就不会感到意外。为什么 UserInfo 不能包含(可选)
address属性? -
@k0pernikus 在运行时,我想为其添加其他属性。
标签: typescript