oop - Unable to create typescript class object -
new typescript, came across typescript class following.
export class errors { errors: { [key: string]: string } = {}; }
i trying create error object manually. can show usage of this? tried following.
errlist: errors = new errors(); errlist = {'key':'value'}; errlist = [ { key: "code1", value: "err1" }, { key: "code2", value: "err2" }, ] errlist.errors = { [key1]:"myvaluex" , [key3]:"myvaluey" };
to initialize class declared have use :
var err = new errors(); err.errors = { "key": "value" };
another alternative define constructor initialize this:
export class errors { constructor(public errors: { [key: string]: string } = {}){}; } var err = new errors({ "key": "value" });
if not have errors on class use interface instead , wouldn't need constructor , use object literals initialize:
export interface errors { errors: { [key: string]: string }; } var err: errors = { errors: { "key": "value" } };
which 1 choose matter of preference , use case. go constructor version, concise initialize lets use instanceof
operator, interface version not.
Comments
Post a Comment