Skip to content

Incorrent type narrowing when using a generic #56077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking β€œSign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
imaliouris opened this issue Oct 12, 2023 Β· 2 comments
Closed

Incorrent type narrowing when using a generic #56077

imaliouris opened this issue Oct 12, 2023 Β· 2 comments
Labels
Duplicate An existing issue was already created

Comments

@imaliouris
Copy link

imaliouris commented Oct 12, 2023 β€’

πŸ”Ž Search Terms

  • generic type infer

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

https://www.typescriptlang.org/play?#code/C4TwDgpgBAggNnAsgewCYTgBQIYEsBOAzgDwAqANFJlBAB7AQB2qhUA1hCMgGZSkB8UALxQA3gCgoUqAG0OIKLkZUAugC4xk6drD5kkfKA3yt2qQDdscAK4QNpOZxWmoAX3GuZmZ+IDGcbEJWAGFsBgBzZHwFCW1fZEZCYHxrX2AogAowawAjOFxfRVQNJPwlcIBKMXd3PwCgvggk4PrWWOl4xOTU9PwMlx1c-MLcYqhS8vIB6Wy8gqhfMIhI6I1QiKiQFyrRGvFxbmtGNNwEqHCIYEw9A1BiAGkaeiYWdk4eRubW-gzRKF19BBDCBKJYbNBXBp4Eg0BgcAQSKQmsAWoFCJR7vwKvZkaigjJ7ipNNpcLwsjcgaBhEIRAByRYbaK0nbTKT4S7WfDKMG2KAAej5qGQTUYtOAUHZhGQcHM0GAAAswlAedBcKxQJAoB91stNlBsMwoAq1YpWIxkOKFdA6JA0hBUFAchBFeZTpyXLVtOzgJzuVZbB59p0kgswgBGYTnS7XQHA34A24gDQAIgZuuiydB-rsUEYEAA7lAdSsQBlaWHma4qgLFOLJdLZerFZb5dBvb6jeBVSEliX9odjsBTsoLlcKcCAEy-f7j0BZ8FuKEIFDoLB4IhkXGtSjyD5Ir5orE4g-43e8fco1pE9pSUlQcmxqk0ulpkvM4lmNkcrnK7P8vkSk0DZNEaza-guJoatA2q9nqBoOsarCQa2Ty2gwDpOi6br4B6+xet+frgoGfgJCGDITpGo4xomU6iAmlJJlAqawRm862BoeaFsWmxlhWFRVv+tZQEKIpioBUoynKYFWoBPo-lBppFixCjwaBYS0k21q0Gh9qOs62Cusg7pAA

πŸ’» Code

type AllModelPairs<T, P extends keyof T> = {
    [key in P]: {
        property: key
        value: T[key]
    }
}[P]

class Category {
    constructor(public id: string) {}
}

class TestClass {
    constructor(
        public id: string,
        public category: Category
    ) {}
}

function getProperty<K extends keyof TestClass>({ property, value }: AllModelPairs<TestClass, K>): TestClass[K] {
    if (property === 'category') {
        return value //doesn't resolve that value is type of Category and this is not the expected behaviour
    }

    return value
}

const cat1 = getProperty({property: "category", value: new Category('1')}) // it resolves that the return type is Category

function getProperty2({ property, value }: AllModelPairs<TestClass, keyof TestClass>): TestClass[keyof TestClass] {
    if (property === 'category') {
        return value // resolves that value is type of Category and this is the expected behaviour
    }

    return value
}

const cat2 = getProperty2({property: "category", value: new Category('1')}) // it doesn't resolve that the return type is Category and that's the expected behaviour

πŸ™ Actual behavior

Typescript doesn't infer that value is type of Category when property is 'category' when property is a generic

πŸ™‚ Expected behavior

Typescript should infer that value is type of Category when property is 'category'

Additional information about the issue

No response

@imaliouris imaliouris changed the title Can't resolve type that comes from a generic Incorrent type inference when using a generic Oct 12, 2023
@jcalz
Copy link
Contributor

jcalz commented Oct 12, 2023

"This is a crash" No, that means the compiler process itself terminated unexpectedly.

This is a known limitation or missing feature. Control flow-based case analysis and generics do not play nicely together. If you use generics, then control flow does not currently do anything to the type parameter: see #33014, #27808, for example.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Oct 12, 2023
@imaliouris imaliouris changed the title Incorrent type inference when using a generic Incorrent type narrowing when using a generic Oct 13, 2023
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants