Skip to content

typeof does not get concatenated string type #20676

Closed
@Lodin

Description

@Lodin

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'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions