In the following code:
export default class SettingsParser {
private static _settings: Settings;
/** Parse commandline, check settings, and init logging to appropriate loglevel */
public static parse(settings: Settings) {
_settings = settings;
}
}
The _settings reference is flagged as an error with the message "[ts] Cannot find name '_settings'. Did you mean the static member 'SettingsParser._settings'?" Yes, I did. C# just infers this. Why can't TypeScript? Having to write SettingsParser. each time is very verbose.
TypeScript just type-checks JavaScript code -- it doesn't alter it. Since your example won't work when run as JavaScript, TypeScript will issue an error.
It alters it a bunch, like making import statements into require statements if necessary. Why not add the class name when it is useful? It doesn't work run as JS but that's not a design goal of TS - the design goal is "valid JavaScript is valid TypeScript", not the other way around.
See https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
Non-goal: Add or rely on run-time type information in programs, or emit different code based on the results of the type system. Instead, encourage programming patterns that do not require run-time metadata.
It alters it a bunch, like making
importstatements intorequirestatements if necessary.
That is transforming one form of valid TypeScript to another form. Transpiling.
That's somewhat splitting hairs. It's changing variable names. Fundamentally it is changing the input code. Why does it matter if it isn't valid JavaScript going in?
@DanielRosenwasser So if it's a "bad idea" to emit this different code, what would one suggest as a good alternative? Do we really have to write out the whole class name every time?
You could just use a let in the scope of the class.
let _settings: Settings;
export default class SettingsParser {
public static parse(settings: Settings) {
_settings = settings;
}
}
The reason we won't emit the class name for you is that that would change the semantics of JavaScript -- setting a member of a class and setting a local variable are two different actions with different syntax. Observe that in this example _settings = settings; does not set a static property of the class, it sets a local variable.
EDIT settings -> _settings
@andy-ms You mean let _settings: Settings;? Is that logically the same as a static member, though?
It's not the same as a static member since it's not a property on the class (classes are objects and can have properties) -- it can only be accessed in its scope. But since you wanted the property to be private anyway, that's probably what you want.
If you need that, there's
let _settings = {};
export default class SettingsParser {
get settings() {
return _settings;
}
set settings(value: typeof _settings) {
_settings = value;
}
}
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
Most helpful comment
It's not the same as a static member since it's not a property on the class (classes are objects and can have properties) -- it can only be accessed in its scope. But since you wanted the property to be
privateanyway, that's probably what you want.