Skip to content

Unary string literal types don't seem to work when applied to function arguments #7541

Closed
@malibuzios

Description

@malibuzios

TypeScript Version:

1.8.7

Code

function func(val: "Test") {
    console.log(val);
}

func("Test");

Expected behavior:
Should work normally and print "Test".

Actual behavior:
A compile-time error is reported (highlighting func):

A signature with an implementation cannot use a string literal type.

In my code I had a function that accepted an argument with a type including only two possible string literal values: say "Value1" and "Value2", e.g function func(val: "Value1" | "Value2"), which worked OK. At some point I decided that "Value2" should not yet be supported so I removed it as an option. This left me with a unary string literal type, e.g. function func(val: "Value1"), but the compiler doesn't seem to like it?

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