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

Allow Default Values for String Literal Types #7223

Closed
blink1073 opened this issue Feb 24, 2016 · 9 comments
Closed

Allow Default Values for String Literal Types #7223

blink1073 opened this issue Feb 24, 2016 · 9 comments
Assignees
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@blink1073
Copy link

TypeScript Version:

1.8.2.

Code

// A self-contained demonstration of the problem follows...
class Foo {
    bar: "fizz" | "buzz";
}

class Bar extends Foo {
    bar = "fizz";
}

Expected behavior:
Bar.bar is initialized to "fizz";

Actual behavior:
Type 'string' is not assignable to type '"buzz"'

@blink1073
Copy link
Author

This works:

interface Foo {
    bar: "fizz" | "buzz"
}

class Bar implements Foo {
    bar: "fizz" | "buzz" = "fizz";
}

@blink1073 blink1073 changed the title Allow Stricter Subtypes for String Literal Types Allow Default Values for String Literal Types Feb 24, 2016
@sccolbert
Copy link

Another example:

class Foo {
    bar: "fizz" | "buzz";
}

class Bar1 extends Foo {
    constructor() {
        super();
        this.bar = "fizz";  // okay
    }
}

class Bar2 extends Foo {
    bar = "fizz";  // fail
}

@DanielRosenwasser
Copy link
Member

I think #6118 should fix this. CC @sandersn

@mhegazy mhegazy added Suggestion An idea for TypeScript Committed The team has roadmapped this issue labels Mar 28, 2016
@sandersn
Copy link
Member

Or #10610

@blink1073
Copy link
Author

I ran the original code through the 2.0.2 compiler, and the error remains.

@blink1073
Copy link
Author

The message has changed to:

test.ts(6,7): error TS2415: Class 'Bar' incorrectly extends base class 'Foo'.
  Types of property 'bar' are incompatible.
    Type 'string' is not assignable to type '"fizz" | "buzz"'.

@sandersn
Copy link
Member

Yep, neither #6118 or #10610 are merged yet. We are trying to determine how bad the breaking change is right now.

@blink1073
Copy link
Author

Ah, fair enough.

@mhegazy mhegazy modified the milestone: Future Sep 21, 2016
@mhegazy mhegazy added In Discussion Not yet reached consensus and removed Committed The team has roadmapped this issue labels Sep 21, 2016
@mhegazy mhegazy removed this from the Future milestone Sep 21, 2016
@RyanCavanaugh
Copy link
Member

Duplicate #10570

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

6 participants