Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String literal type in computed property of object literal doesn't validate type #16208

Closed
greg-hornby-roam opened this issue Jun 1, 2017 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@greg-hornby-roam
Copy link

greg-hornby-roam commented Jun 1, 2017

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

var UserFields = {
    "name": <"name">"name",
    "favColor": <"favColor">"favColor"
};

interface UserInterface {
    name: string;
    favColor: "red" | "green" | "blue"
}


//Example 1

var newUser: Partial<UserInterface> = {};
//Example 1.1 - Expect: valid, result: valid - Correct
newUser[UserFields.favColor] = "red";
//Example 1.2 - Expect: invalid, result: invalid - Correct
newUser[UserFields.favColor] = "yellow";



//Example 2
//Example 2.1 - Expect: valid, result: valid - Correct
var newUser: Partial<UserInterface> = {
    [UserFields.favColor]: "red"
}

//Example 2.2 - Expect: invalid, result: valid - Incorrect
var newUser: Partial<UserInterface> = {
    [UserFields.favColor]: "yellow"
}

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

@greg-hornby-roam 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
@Roam-Cooper
Copy link

👍

@mhegazy
Copy link
Contributor

mhegazy commented Aug 24, 2017

looks like a duplicate of #16687

@mhegazy mhegazy added the Duplicate An existing issue was already created label Aug 24, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Sep 8, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed Sep 8, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants