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
In my project we generate an object of fields per database model, with each field cast to a string literal of the field name. We also generate an interface to map the field names to types.
In this example I wrote up, a User has fields "name" and "favColor", "name" is simply a string, and "favColor" is an enum of red,green,blue.
In example 1.2 when we try to assign "yellow" to "favColor" typescript correctly errors as yellow is not assignable to the enum.
However in example 2.2, doing a similar thing, but as a computer property in the object literal, it does not correctly error when trying to assign "yellow" to "favColor"
The text was updated successfully, but these errors were encountered:
greg-hornby-roam
changed the title
String literal in computed property of object literal doesn't validate type
String literal type in computed property of object literal doesn't validate type
Jun 1, 2017
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
TypeScript Version: 2.3.2
In my project we generate an object of fields per database model, with each field cast to a string literal of the field name. We also generate an interface to map the field names to types.
In this example I wrote up, a User has fields "name" and "favColor", "name" is simply a string, and "favColor" is an enum of red,green,blue.
In example 1.2 when we try to assign "yellow" to "favColor" typescript correctly errors as yellow is not assignable to the enum.
However in example 2.2, doing a similar thing, but as a computer property in the object literal, it does not correctly error when trying to assign "yellow" to "favColor"
Code
http://www.typescriptlang.org/play/index.html#src=var%20UserFields%20%3D%20%7B%0A%20%20%20%20%22name%22%3A%20%3C%22name%22%3E%22name%22%2C%0A%20%20%20%20%22favColor%22%3A%20%3C%22favColor%22%3E%22favColor%22%0A%7D%3B%0A%0Ainterface%20UserInterface%20%7B%0A%20%20%20%20name%3A%20string%3B%0A%20%20%20%20favColor%3A%20%22red%22%20%7C%20%22green%22%20%7C%20%22blue%22%0A%7D%0A%0A%0A%2F%2FExample%201%0A%0Avar%20newUser%3A%20Partial%3CUserInterface%3E%20%3D%20%7B%7D%3B%0A%2F%2FExample%201.1%20-%20Expect%3A%20valid%2C%20result%3A%20valid%20-%20Correct%0AnewUser%5BUserFields.favColor%5D%20%3D%20%22red%22%3B%0A%2F%2FExample%201.2%20-%20Expect%3A%20invalid%2C%20result%3A%20invalid%20-%20Correct%0AnewUser%5BUserFields.favColor%5D%20%3D%20%22yellow%22%3B%0A%0A%0A%0A%2F%2FExample%202%0A%2F%2FExample%202.1%20-%20Expect%3A%20valid%2C%20result%3A%20valid%20-%20Correct%0Avar%20newUser%3A%20Partial%3CUserInterface%3E%20%3D%20%7B%0A%20%20%20%20%5BUserFields.favColor%5D%3A%20%22red%22%0A%7D%0A%0A%2F%2FExample%202.2%20-%20Expect%3A%20invalid%2C%20result%3A%20valid%20-%20Incorrect%0Avar%20newUser%3A%20Partial%3CUserInterface%3E%20%3D%20%7B%0A%20%20%20%20%5BUserFields.favColor%5D%3A%20%22yellow%22%0A%7D
The text was updated successfully, but these errors were encountered: