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

Strict object literal check fails when infered to type containing string literal type #7085

Closed
erichillah opened this issue Feb 15, 2016 · 1 comment
Labels
Question An issue which isn't directly actionable in code

Comments

@erichillah
Copy link

Hi,
This issue might be linked to #6167 ( particularly widening i guess) or #6554, but i did not find any details the bellow case:

interface Test {
    value: string;
    comparator:"equals"|"isGreater";
}

function applyTest(value: string, testval:Test) {
    return "ok";
}

let tt = {value: "test", comparator:"equals"};
applyTest("ko",tt);

when i compile using tsc (typescript@1.9.0-dev.20160215) i get a warning

test.ts(11,16): error TS2345: Argument of type '{ value: string; comparator: string; }' is not assignable to parameter of type 'Test'.
  Types of property 'comparator' are incompatible.
    Type 'string' is not assignable to type '"equals" | "isGreater"'.
      Type 'string' is not assignable to type '"isGreater"'.
@weswigham
Copy link
Member

Apply a type annotation such as let tt: Test = { ... }. TS only infers string literal types in type positions and when contextually appropriate based on type information. Since TS doesn't do use-site analysis, tt gets typed soled by its definition, which is {value: string, comparator: string}, as there's no casts/annotations indicating it should use a literal type.

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Feb 16, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants