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

typeof does not get concatenated string type #20676

Closed
Lodin opened this issue Dec 13, 2017 · 3 comments
Closed

typeof does not get concatenated string type #20676

Lodin opened this issue Dec 13, 2017 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@Lodin
Copy link

Lodin commented Dec 13, 2017

The pattern I used in code below is originally from Redux. When I use strings "as is", without any concatenation, it works. But when I try to move common part (prefix) out, it brokes. Intuitively I assume that since prefix and other parts are constants typeof should get united type.

TypeScript Version: 2.7.0-dev.20171213

Code

const prefix = 'prefix';

const TOKEN1 = `${prefix}/token1`;
const TOKEN2 = `${prefix}/token2`;

interface Wrapper {
    type: string;
}

interface WrapperToken1 extends Wrapper { 
    test: string;
    type: typeof TOKEN1;
}

interface WrapperToken2 extends Wrapper {
    other: number;
    type: typeof TOKEN2;
 }

function test(wrapper: WrapperToken1 | WrapperToken2): string { 
    switch (wrapper.type) {
        case TOKEN1:
            return wrapper.test;
        case TOKEN2:
            return wrapper.other.toString();
        default:
            return '';
    }
}

Expected behavior:
No errors raised.

Actual behavior:
Raises errors:

Property 'test' does not exist on type 'WrapperToken1 | WrapperToken2'.
  Property 'test' does not exist on type 'WrapperToken2'.

Property 'other' does not exist on type 'WrapperToken1 | WrapperToken2'.
  Property 'other' does not exist on type 'WrapperToken1'.
@ghost
Copy link

ghost commented Dec 13, 2017

Duplicate of #12940 -- currently we never compute string literal types for concatenations.

@ghost ghost added the Duplicate An existing issue was already created label Dec 13, 2017
@Lodin
Copy link
Author

Lodin commented Dec 13, 2017

@andy-ms Thanks! Should I close this issue?

@ghost
Copy link

ghost commented Dec 13, 2017

I think so, you could add your use case as a comment to that issue.

@Lodin Lodin closed this as completed Dec 13, 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

1 participant