You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently the following snippet generates a type error: let foo: xyz = {}
My proposal is the whenever a variable is declared to contain only one possible value, that value should be assumed whenever the variable's value is not specified. So, the above snippet would then not generate an error, but this would: let foo: xyz = { num: 2 }
This proposed feature will make it easier to write clearer and simpler TypeScript code.
The text was updated successfully, but these errors were encountered:
I don't quite understand what is meant "that would change the semantics of JavaScript". All TypeScript is a type checker; what I mean is that for purposes of type checking, the variable should be assumed to be of the proper type if it does not have a value assigned. This should not have any effect on the code generated.
In fact what I'm proposal is already done in TypeScript to some extent. Consider this code snippet
let xyz = class {
x: string
y: "ok"
z: number
}
let ok = new xyz().y
This code does not generate an error, although new xyz().y , and hence ok do not have any value assigned
Oh, I thought you meant to fill the value in.
I don't wee why we should allow you to omit a field of a literal type, though. If you access the field you will get an error at runtime because you get undefined instead of the value you were expecting.
As for the second error you show, see #20166.
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
Consider the interface:
Currently the following snippet generates a type error:
let foo: xyz = {}
My proposal is the whenever a variable is declared to contain only one possible value, that value should be assumed whenever the variable's value is not specified. So, the above snippet would then not generate an error, but this would:
let foo: xyz = { num: 2 }
This proposed feature will make it easier to write clearer and simpler TypeScript code.
The text was updated successfully, but these errors were encountered: