Skip to content

Using "implements" causes an issue with string literal types. "extends" does not. #10071

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

Closed
dsifford opened this issue Aug 1, 2016 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@dsifford
Copy link
Contributor

dsifford commented Aug 1, 2016

TypeScript Version: nightly (2.0.0-dev.201xxxxx)

Relevant StackOverflow discussion

Playground

Kudos to @nitzantomer for looking into this

Code

// This is defined in a d.ts file. 
class Test {
    someObj: {
        someString: 'this'|'that'|'the other'
    };
}

// This is me actually using the class.
class Test2 implements Test {
    someObj = {
        someString: 'this'
    }
}

Expected behavior:
No error thrown. Using literal 'this' as the value for someObj.someString correctly implements class.

Actual behavior:
Error thrown.

Class 'Test2' incorrectly implements interface 'Test'.
  Types of property 'someObj' are incompatible.
    Type '{ someString: string; }' is not assignable to type '{ someString: "this" | "that" | "the other"; }'.
      Types of property 'someString' are incompatible.
        Type 'string' is not assignable to type '"this" | "that" | "the other"'.
          Type 'string' is not assignable to type '"the other"'.
@weswigham
Copy link
Member

weswigham commented Aug 1, 2016

@sandersn Was looking into a related issue before - contextually typing inherited methods - I believe this is similar in nature.

@DanielRosenwasser
Copy link
Member

Duplicate of #1373 and #3667.

See @sandersn's PR and explanation of why we couldn't do this at #6118 (comment).

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Aug 3, 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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants