-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Type parameter's function properties referencing other properties of the same parameter are broken #60848
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
Comments
TS inference algorithm can't always unify everything in ways you'd like it to when some information is missing. It has to deduce it after all and sometimes it might fail. That's why you see different results when you annotate that parameter vs when you don't. With annotated parameters, TS is able to infer some things earlier - in a single inference pass. I'm surprised by the different behavior of your arrow function vs method examples. Both are context-sensitive and I'd expect them to behave roughly the same way in this context. That said, your first example can be fixed if you use a reverse-mapped type (TS playground): namespace round_four {
declare function define<
const obj extends {
readonly primaryProperty: number;
dependentProperty: obj["primaryProperty"];
dependentFunctionProperty?: (primary: obj["primaryProperty"]) => void;
},
>(obj: { [K in keyof obj]: obj[K] }): obj;
define({
primaryProperty: 11,
dependentProperty: 11, // no errors
});
define({
primaryProperty: 11,
dependentProperty: 13, // an error
});
define({
primaryProperty: 11,
dependentProperty: 11,
dependentFunctionProperty: () => {}, // no errors if there is no parameter
});
define({
primaryProperty: 11,
dependentProperty: 11,
dependentFunctionProperty: (primary) => {
primary; // nicely inferred as 11, even when `primary` parameter isn't annotated
},
});
} |
Ah, the method variant typechecks in ur case only because its typed using a method syntax and methods are bivariant |
Thank you! Should this issue be closed as not planned now? I still wonder why redeclaration has effect on the types, though at this point that is not a practical interest |
This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
π Search Terms
generics, type parameters, dependent properties
π Version & Regression Information
None of the versions I have tried work as expected.
β― Playground Link
https://www.typescriptlang.org/play/?ts=5.1.6#code/HYQwtgpgzgDiDGEAEAnA9gV2AEwPpuGQG8AoJcpAekqQBsIAXAciiSgZBQaQHcBLBgAskIJADMs8BnwJIhIBgC4yFakgC0I+IhgNWo4Bkgo+8JAAMYJsJwCeABXQwIXW+ZXk1mhDr0XsEM44EMAMjmjOrua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mBdKDuOlFBCll5FIDe2b6jQ7N1fBsw2IYuTHa2FOmYUjHDPMmC8cAsLtcJEBgBm940OZYy2hgySwVxYAwGgUD8Rb4h2kgABEm4oDYtC+0tfRsAJoAMBg4LOn8rrSENFji5L0PoQW-YI2dBEoyRCXIxjM5MtjH2SSOBNJqe7bpv28OMbLOVKx5usJzaJN2Ib3WGr11onHu1PgqEB6tiO9VQI7fgOcPVd02+XNoXm6dYZnyLZwXMU1mjq9EYXWPvWguOlyGABM5dqFP24dlLAZ11h2XXreeUkAV0BwE1pU4LgQjgsQjeyXVo3cAYAUIQ1kpoBHeEsTa+4QEDk+PQMAc0bZaHgJkJkWtYR8CYGaXuUBXhmnBCkeQxligwGOuCZwChrSNxgacc4yww5oH3OYVahBog2HtNASyzhcEmF0LBQQIBsG5DAO7bgrxtx2RhPNOhlwWQrRhutYA7wtr-EBDgYE-YDrQjNg9BK4sc43U3ndMkujEZPTXtdZkrUQKI05IlUuxjl5Un+lKNiSkVQg2+JqCG3xpGLSuMtO46FFHKO+NtNR2ANFYS0UdHRv09FGAxAYjeWdHGmJFOY7e684ozCDHYvGfIwYkhMfEsxLiVzsWwB49UXiik+J+FDSsac9pFnSSvfOWTLGkVHOpGglcL7piQIecEbMnxDLQPgnuEy1htwCmwRIPB6bJxnrVXSb5wR3AWiYROzCd5WOWkGaIGwep7yqRY6sOSbFZwKSlVxLT0oy1vnLBWjSWzAGbj3WZbhbQOlYLQPgHCuKzF2evYu+8jmsF0nPbszTCbX1KVnDp-pMYpPaT0zpFy5GLkPiGVKFctw7hrtLG+FY74UBeTChe9za4IrRXLbs69un0uhtkrFCUbmhhPncogbzfgDKJfCzKTycrKiAA
π» Code
π Actual behavior
I'm not sure where to begin. Properties of a type parameter that reference other properties of the same type parameter seem to be completely untested in TypeScript.
In the example above, I have referenced a generic's property from other properties in three ways:
All of them are buggy, but in different ways.
π Expected behavior
Speaking honestly, I was expecting from such dependent properties to not work at all, failing with some kind of circular constraint error, but now I believe they could become usable some day.
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: