Skip to content

[Literals][Indexes][Type assertion] Is that a bug? #14093

Closed
@hinell

Description

@hinell

Literal types related topics & conversations

Related to this issue PR that regulates literal type inference and its behavior in self-type changing can be found here: #10676

TypeScript Version: 2.1.6

Code

interface T {[key: string]: 'literal'}
let  FOO: (obj: T) => T;
let DOOMED = { prop:'literal' } // DOOMED.prop === 'string' but not the literal

FOO(DOOMED)         // fails as expected
FOO(<T>DOOMED)      // fails
FOO(DOOMED as T)    // fails!

let SAVED: T = { prop:'literal' }
FOO(SAVED) // ok

Expected behavior:
Type assertion is expected to work in both cases <> and as but doesn't

Actual behavior:
Fails

Error:

FOO(<T>DOOMED)      // fails
       ~~~~~~~~~

error TS2352: Type '{ prop: string; }' cannot be converted to type 'T'.
  Property 'prop' is incompatible with index signature.
    Type 'string' is not comparable to type '"literal"'.


FOO(DOOMED as T)    // fails!
       ~~~~~~~~~~~

error TS2352: Type '{ prop: string; }' cannot be converted to type 'T'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions