Reputation: 7369
I have the next class in TypeScript:
export class A {
public a:boolean = false;
public b:boolean;
public c:string = null;
public d:string;
}
...
let some = new A();
console.log(Object.keys(some)) // Returns only [a,c]
I am interested to get all variables name, assigned and unassigned, and after to get the type of that variable.
Why Object.keys is not giving me all propertes? // my valid result is: a,b,c,d
Upvotes: 0
Views: 92
Reputation: 61
Typescript is just superset compiled back to plain javascript. It provides extra typechecking during compilation and can help with intellisense in some IDEs and editors.
You have just let typescript know, that your object A will only ever have variables a,b,c,d
and then assigned a
and c
some values. If you would tried to assign to variable e
on your object: some.e = false
it would fail during compilation.
But in the end it was compiled to regular javascript and variables b
and d
simply don't exist.
Upvotes: 1
Reputation: 1014
Because b
and d
are never initialized in the runtime, so they don't exist. You have to initialize them yourself, which is a good thing to do (depending on your case, I can't know).
Note that in your tsconfig.json
you can force the property to be initialized by yourself
{
"strictPropertyInitialization": true
}
Already at true
if you're in strict mode.
Upvotes: 1