GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Function parameter type inference error · Issue #59949 · microsoft/TypeScript

Function parameter type inference error #59949

New issue
Copy link
New issue
Copy link
Closed
Closed
Function parameter type inference error#59949
Copy link

Description

@lengyuxuan
fangjin (lengyuxuan)
opened on Sep 12, 2024
Issue body actions

🔎 Search Terms

Function parameter

🕗 Version & Regression Information

  • This changed between versions ______ and _______
  • This changed in commit or PR _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______

⏯ Playground Link

https://www.typescriptlang.org/zh/play/?#code/KYOwrgtgBAKgngB2FA3gWAFBSgQQDSbYBCBGAvppgJYgAuwATgGYCGAxsvEgLIsKqEoAbS7AAdDgC6ALigBnWgxoBzANyCRicURlRwEAEaN15ShlpaoADQA8MKMAAe9EABM5sLQD4oAXmyivAgikiaYTGAgbLRUAPYgUBFRdg7OoO6eSF4AFBZIsjB4qS6usrYwXgCUAlhQVExQuZa+LZniONXotdgA9D1QAOoAFnBQAEROJWNQechUHiCxtPKKKlAA-IK9-dkILAwsEMD0DNWT6bIKSiDKUAA+epBGDPdQBrGxADbALCBbUGx4nIvuJPrFlE0kEVzm5KiZsBRTBhwpE2NlRBIigByLFwlFRdFaMQkKAARjhQA

💻 Code

enum Type {
  A,
  B,
}

interface TypeMap {
  [Type.A]: string;
  [Type.B]: number;
}

type X<T extends Type> =  TypeMap[T];

function func<T extends Type>(type: T, extend: X<T>) {
  if (type === Type.A) {
    // Why "extend" type is not string ?
    // (parameter) extend: string | number | boolean
    console.log(type, extend);
  }
}

func(Type.A, '');
func(Type.B, 1);

🙁 Actual behavior

In the example, Why "extend" type is not string ?

🙂 Expected behavior

The "type" constrained to "Type.A",so "extend" shuld be "string".

Additional information about the issue

No response

Activity

  1. MartinJohns commented on Sep 12, 2024

    @MartinJohns
    Martin Johns (MartinJohns)
    on Sep 12, 2024
    Contributor
    More actions

    Because checking what value type is does not have any relevance for what type the value of extend is.

    This is a perfectly valid call: func<Type.A | Type.B>(Type.A, 1);

  2. lengyuxuan commented on Sep 12, 2024

    @lengyuxuan
    fangjin (lengyuxuan)
    on Sep 12, 2024
    Author
    More actions

    Because checking what value type is does not have any relevance for what type the value of extend is.

    This is a perfectly valid call: func<Type.A | Type.B>(Type.A, 1);

    How should I constrain the type of "extend" ? Do you have any suggestions ? Thank you so much !

  3. jcalz commented on Sep 12, 2024

    @jcalz
    Joe Calzaretta (jcalz)
    on Sep 12, 2024
    Contributor
    More actions

    Duplicate #33014

    For now, if you need this behavior you either need to rewrite in terms of #47109 and use generic indexing instead of control flow anlaysis:

    function func<T extends Type>(type: T, extend: X<T>) {
        const x: { [K in Type]: (type: K, x: X<K>) => void } = {
            [Type.A](type, extend) {
                console.log(type, extend);
                //                ^? (parameter) extend: string
            },
            [Type.B](type, extend) {
    
            }
        }
        x[type](type, extend);
    }
    func(Type.A, '');
    func(Type.B, 1);
    func(Math.random() < 0.99 ? Type.A : Type.B, 1); // still allowed, #47109 technically unsound this way

    or, you need to use control flow analysis on a discriminated union and not use generics:

    function func(...[type, extend]: { [T in Type]: [type: T, extend: X<T>] }[Type]) {
        if (type === Type.A) {
            console.log(type, extend);
            //                ^? (parameter) extend: string
        }
    }
    
    func(Type.A, '');
    func(Type.B, 1);
    func(Math.random() < 0.99 ? Type.A : Type.B, 1); // error

    Playground link to code

    Reacted by Ryan Cavanaugh and fangjin
  4. lengyuxuan commented on Sep 13, 2024

    @lengyuxuan
    fangjin (lengyuxuan)
    on Sep 13, 2024
    Author
    More actions

    Duplicate #33014

    For now, if you need this behavior you either need to rewrite in terms of #47109 and use generic indexing instead of control flow anlaysis:

    function func<T extends Type>(type: T, extend: X<T>) {
        const x: { [K in Type]: (type: K, x: X<K>) => void } = {
            [Type.A](type, extend) {
                console.log(type, extend);
                //                ^? (parameter) extend: string
            },
            [Type.B](type, extend) {
    
            }
        }
        x[type](type, extend);
    }
    func(Type.A, '');
    func(Type.B, 1);
    func(Math.random() < 0.99 ? Type.A : Type.B, 1); // still allowed, #47109 technically unsound this way

    or, you need to use control flow analysis on a discriminated union and not use generics:

    function func(...[type, extend]: { [T in Type]: [type: T, extend: X<T>] }[Type]) {
        if (type === Type.A) {
            console.log(type, extend);
            //                ^? (parameter) extend: string
        }
    }
    
    func(Type.A, '');
    func(Type.B, 1);
    func(Math.random() < 0.99 ? Type.A : Type.B, 1); // error

    Playground link to code

    Good job

  5. fangjin (lengyuxuan)
    closed this as completedon Sep 13, 2024
  6. fangjin (lengyuxuan)
    reopened this on Sep 13, 2024
  7. fangjin (lengyuxuan)
    closed this as completedon Sep 13, 2024
  8. microsoft
    locked as resolved and limited conversation to collaborators on Oct 22, 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

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.