Skip to content

Inconsistent String Literal Type Propagation #15271

@mattnedrich

Description

@mattnedrich

TypeScript Version: 2.3.0 RC

I'm observing some strange type behavior. Consider the following snippet.

type myType = "MY_TYPE"; const myType: myType = "MY_TYPE"; const foo = myType; const bar = { baz: foo };

The type of foo and baz are both "MY_TYPE" as expected.

However using this strEnum function as described here which, based on my understanding, should be doing the same thing as the above code, results in something different:

function strEnum<T extends string>(o: Array<T>): {[K in T]: K} { return o.reduce((res, key) => { res[key] = key; return res; }, Object.create(null)); } const myTypeEnum = strEnum(["MY_TYPE"]) const foo = myTypeEnum.MY_TYPE; const bar = { baz: foo };

Here, foo is detected to be type "MY_TYPE", but baz is recognized as type string

Am I doing something wrong? Is this expected behavior?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions