You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
typeAllModelPairs<T,PextendskeyofT>={[keyinP]: {property: keyvalue: T[key]}}[P]classCategory{constructor(publicid: string){}}classTestClass{constructor(publicid: string,publiccategory: Category){}}functiongetProperty<KextendskeyofTestClass>({ property, value }: AllModelPairs<TestClass,K>): TestClass[K]{if(property==='category'){returnvalue//doesn't resolve that value is type of Category and this is not the expected behaviour}returnvalue}constcat1=getProperty({property: "category",value: newCategory('1')})// it resolves that the return type is CategoryfunctiongetProperty2({ property, value }: AllModelPairs<TestClass,keyofTestClass>): TestClass[keyofTestClass]{if(property==='category'){returnvalue// resolves that value is type of Category and this is the expected behaviour}returnvalue}constcat2=getProperty2({property: "category",value: newCategory('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
The text was updated successfully, but these errors were encountered:
imaliouris
changed the title
Can't resolve type that comes from a generic
Incorrent type inference when using a generic
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.
π Search Terms
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play?#code/C4TwDgpgBAggNnAsgewCYTgBQIYEsBOAzgDwAqANFJlBAB7AQB2qhUA1hCMgGZSkB8UALxQA3gCgoUqAG0OIKLkZUAugC4xk6drD5kkfKA3yt2qQDdscAK4QNpOZxWmoAX3GuZmZ+IDGcbEJWAGFsBgBzZHwFCW1fZEZCYHxrX2AogAowawAjOFxfRVQNJPwlcIBKMXd3PwCgvggk4PrWWOl4xOTU9PwMlx1c-MLcYqhS8vIB6Wy8gqhfMIhI6I1QiKiQFyrRGvFxbmtGNNwEqHCIYEw9A1BiAGkaeiYWdk4eRubW-gzRKF19BBDCBKJYbNBXBp4Eg0BgcAQSKQmsAWoFCJR7vwKvZkaigjJ7ipNNpcLwsjcgaBhEIRAByRYbaK0nbTKT4S7WfDKMG2KAAej5qGQTUYtOAUHZhGQcHM0GAAAswlAedBcKxQJAoB91stNlBsMwoAq1YpWIxkOKFdA6JA0hBUFAchBFeZTpyXLVtOzgJzuVZbB59p0kgswgBGYTnS7XQHA34A24gDQAIgZuuiydB-rsUEYEAA7lAdSsQBlaWHma4qgLFOLJdLZerFZb5dBvb6jeBVSEliX9odjsBTsoLlcKcCAEy-f7j0BZ8FuKEIFDoLB4IhkXGtSjyD5Ir5orE4g-43e8fco1pE9pSUlQcmxqk0ulpkvM4lmNkcrnK7P8vkSk0DZNEaza-guJoatA2q9nqBoOsarCQa2Ty2gwDpOi6br4B6+xet+frgoGfgJCGDITpGo4xomU6iAmlJJlAqawRm862BoeaFsWmxlhWFRVv+tZQEKIpioBUoynKYFWoBPo-lBppFixCjwaBYS0k21q0Gh9qOs62Cusg7pAA
π» Code
π 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
The text was updated successfully, but these errors were encountered: