Skip to content

Tag template of arrow function infers wrong type #36700

Closed

Description

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: []

Activity

  1. Zemnmez commented on Feb 8, 2020

    @Zemnmez

    It looks like this type inferrence is at least consistent with calling the same function without using template syntax:

    {
        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!
    }
    
    {
        const tag = (strings: TemplateStringsArray, ...args: unknown[]) => [strings, args];
    
        // 🛑 inferred type: (TemplateStringsArray | unknown[])[]
        const t = tag(Object.assign(["123"], {raw: ["123"]}));
    
        console.log(t[2]); // should not compile, but it does!
    }

    Playground Link

  2. danieldietrich commented on Feb 8, 2020

    @danieldietrich
    Author

    That's right.

    An additional type witness does solve the problem. But it should work without it.

    {
        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!
    }
    
    {
        const tag = (strings: TemplateStringsArray, ...args: unknown[]): [TemplateStringsArray, unknown[]] => [strings, args];
    
        // 🛑 inferred type: (TemplateStringsArray | unknown[])[]
        const t = tag`123`;
    
        console.log(t[2]); // it is correct that this does not compile
    }

    Playground Link

  3. Zemnmez commented on Feb 8, 2020

    @Zemnmez

    I think this is down to typescript not inferring tuple return types at all, and therefore not being able to correctly intuit the response here. I think this is a feature request, rather than a bug then.

    Consider this minimal example:

    {
        // f: () => number[], rather than f: () => [1,2]
        const f = () => {
            return [1,2]
        }
    }

    Playground Link

  4. danieldietrich commented on Feb 8, 2020

    @danieldietrich
    Author

    You are absolutely right. Without an additional type hint, type inference for a plain function behaves exactly the same. I will close this issue (and I will not create a feature request).

    Many thanks for your investigation!

    {
        // f: () => number[], rather than f: () => [1,2]
        const f = () => {
            return [1,2]
        }
    }
    
    {
        // f(): number[], perfectly consistent
        function f() {
            return [1,2]
        }
    }

    Playground Link

  5. Zemnmez commented on Feb 8, 2020

    @Zemnmez

    np! I would love to see this inference too :)

  6. danieldietrich commented on Feb 8, 2020

    @danieldietrich
    Author

    I think it would be risky, the change might not be source compatible regarding existing code bases. But currently I can't find a scenario where it would break existing code.

    function f(ns: number[]) { }
    
    const arr: [1, 2] = [1, 2]
    
    f(arr); // works!

    Playground Link

  7. Zemnmez commented on Feb 8, 2020

    @Zemnmez

    looks like this is issue #25516

  8. danieldietrich commented on Feb 8, 2020

    @danieldietrich
    Author

    Yes, it is. I would love to see more precise type inference.

  9. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions