Skip to content

Tag template of arrow function infers wrong type #36700

@danieldietrich

Description

@danieldietrich

TypeScript Version: 3.8.0-dev.20200208

Search Terms: tag function, tagged template, template string

Code

{
    function tag(strings: TemplateStringsArray, ...args: unknown[]): [TemplateStringsArray, unknown[]] {
        return [strings, args];
    }

    // ✅ inferred type: [TemplateStringsArray, unknown[]]
    const t = tag`123`;

    // console.log(t[2]); // it is correct that this would not compile
}

{
    const tag = (strings: TemplateStringsArray, ...args: unknown[]) => [strings, args];

    // 🛑 inferred type: (TemplateStringsArray | unknown[])[]
    const t = tag`123`;

    console.log(t[2]); // should not compile, but it does!
}

Expected behavior:

The console.log(t[2]) statement should produce the following error:

Tuple type '[TemplateStringsArray, unknown[]]' of length '2' has no element at index '2'. ts(2493)

Actual behavior:

The example does compile (but it shouldn't).

Playground Link: here

Related Issues: []

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions