私は次のクラスを持っています:
export class SomeModel {
prop1: number;
prop2: number;
comment: string;
}
そして、そのプロパティを動的に取得する次のメソッド:
getTypeProperties<T>(obj: T): string[] {
const ret: string[] = [];
for (const key in obj) {
if (obj.hasOwnProperty(key))
ret.push(key);
}
return ret;
}
次の呼び出しは、空の配列を返します。
getTypeProperties(new SomeModel());
ただし、すべてのプロパティを で明示的に初期化するとnull
、プロパティは正しく返されます。
export class SomeModel {
prop1: number = null;
prop2: number = null;
comment: string = null;
}
質問:これは正常な動作ですか? または、これを切り替える TypeScript コンパイラ スイッチがありますか?
関連するかどうかはわかりませんが、tsconfig.json の内容は次のとおりです。
{
"compileOnSave": false,
"compilerOptions": {
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2017",
"dom"
]
}
}