-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Type inference with destructured parameters is overly specific #55596
Comments
Duplicate of #51179. |
@MartinJohns I see the relation, but don't see how it's duplicated. That one talks about explicitly stated types while this is talking about type inference. |
The type is inferred by the default value you provide, which is of type So your parameter argument is typed |
@MartinJohns Thanks. I see that's much more related and almost identical to what I've presented. I do wonder what the consequences of narrowing the type would be. I understand it's not a bug, though I would like the ability to narrow this somehow, even if it's behind a flag. I can do a post-processing of the typings file and rewrite, though I wish I didn't have to. I don't see any consequence of refactoring: declare function getFoo({ foo }?: {foo:string, bar:boolean): string; as declare function getFoo({ foo }?: {foo:string): string; I was presenting a limited example of the issue I see, but a more detailed explanation of what I'm trying to accomplish is here function extendObject<
T = any,
M = Record<string, (source?: any) => string | boolean | null>
>(target: T, methods: M):
T & { [K in keyof M & string]: M[K] extends (...args: infer R) => any ?
(data?: R[0]) => ReturnType<M[K]> : never } {
// @ts-expect-error testing
for (const [key, value] of Object.entries(methods)) {
// @ts-expect-error testing
t[key] = value.bind(t);
}
// @ts-expect-error testing
return t;
}
const methods = {
method1({ ATTRIBUTE_NODE } = document) {
return ATTRIBUTE_NODE;
},
method2({ innerWidth} = window) {
return window.innerWidth;
}
}
const result = extendObject({}, methods);
result.method1(); // Optional param0
result.method1(document); // Redundant param0
result.method1({ ATTRIBUTE_NODE: 2 }) // param0-like (Error)
result.method2(); // Optional param0
result.method2(window); // Redundant param0
result.method2({ innerWidth: 360 }) // param0-like (Error) Essentially I want to manipulate an object and in some cases the prototype directly. It's related to The real world example is here: https://github.com/clshortfuse/materialdesignweb/blob/42d3679026733648908758ae5f0d17d63db9ce99/components/Button.js#L57-L64 It all works with the types I have built and automatically through inference. But I have to strip the type because it goes on this extremely long, recursive type that leaves files ending up in clshortfuse/materialdesignweb@bc34a31#diff-bd37c6657f46f2ec3dfb03aaa075b8a802a10da7e6a8c8b59363c6e9c8e5fb3bL383-L1145 (It's the If I could infer the destructured keys used from the object I could rewrite/optimize the resulting type myself, but I don't think there exists a utility type for listing destructured keys (eg: |
If the team introduced a flag every time it's suggested I would quickly run out of disk space for the tsconfig alone. You can use a type annotation and { foo }: Pick<typeof defaultInstanceOfP, "foo"> = defaultInstanceOfP Inferring a different type based on the destructuring is not going to happen. |
That means every single expression would have to redundantly be rewritten, which is the original point. Also, I would suggest you continue to read the rest to comment to why the sample code you provided doesn't work.
It's contradictory to state that disk space is a concern, when implementing the flag in question would reduce file space. Right now the overly specific types are resulting the types folder being @RyanCavanaugh Let me know what you think about possible solution for tackling this issue. It's slightly tied to another issue related to recursion that tagged for I was able to mostly reconstruct the code base to avoid heavy recursion, but kinda stuck on this one. |
This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
π Search Terms
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play?ts=5.3.0-dev.20230831#code/MYGwhgzhAEAK0G8CwAoa7oDMD23oF5oByAIzACciBuVDabAFwAsBTc2CsAWwOIDtG5FgEcArgEshAE2qoAvqlTBsfCA2hSWmMKJAMAkqoZg+wFgHlM8QnxYB3OAAoAlDRSpMo0w3EroAcxYGADFcRwQsXGg5Xk1tXQMjEzNLWGdEWgwhBlFyPkjsNwV3FECQsIicbAAuYjIALyJo51QgA
π» Code
π Actual behavior
π Expected behavior
Additional information about the issue
This is just an example, but having to cast it on the function appears needless to me. If
P
is actually required because, perhaps, the code will referencearguments[0]
, then that should be the explicit cast needed (as P
), not other way around.In more complex setups, destructing many variables with a cast is needlessly verbose:
The text was updated successfully, but these errors were encountered: