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
When a function only depends on a subset of the object properties of an argument, narrow the type.
π Motivating Example
interfaceX{a: number;b: number;c: number;}functionf({ a }: X){// ...}
In this example, the function f does not depend on the entire X, but instead on a subset of that type, e.g. Pick<X, "a">.
While you could manually specify this type, there is no reason for any function to depend on properties that it can't even access in the first place - I would therefore suggest that the subset of that type be automatically determined and inferred, resulting in a function that doesn't demand values it can't actually either see or use.
π» Use Cases
In general, there is no point in passing arguments to functions that can't even see them - destructuring hides the original object, and the function only receives the individual properties; since it can't access the object itself, it ultimately doesn't matter what the type of that argument is.
If you had to, for example, mock the arguments to a function for testing, this makes that much easier to - in the example, you can simply call f({ a: 1 }) and that would be valid, without have to resort to as any, or explicitly type-hinting with Pick, neither of which make much sense.
β Caveat
This being JavaScript, you can of course access the object using arguments[0] - so the type of this object could in fact matter.
However, the type of arguments[0] is always any, and so, in that case, there's no type safety either way; if the rest of the type is important, you probably aren't (and likely shouldn't be) destructuring the argument in the first place.
So this seems unlikely to affect anything other than things like currying and higher-order functions, where you wouldn't be destructing anyway - and so, it seems unlikely this will cause any adverse effects in practice.
What do you think?
The text was updated successfully, but these errors were encountered:
Parameter destructuring is considered to be an implementation detail, not an externally-visible facet of the function signature, so we don't modify the callee's view of the call based on whether or not the function uses destructuring.
edit: Actually I'd say this is a duplicate of #42419.
Suggestion
π Search Terms
destructuring, narrowing, parameters, arguments
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
When a function only depends on a subset of the object properties of an argument, narrow the type.
π Motivating Example
In this example, the function
f
does not depend on the entireX
, but instead on a subset of that type, e.g.Pick<X, "a">
.While you could manually specify this type, there is no reason for any function to depend on properties that it can't even access in the first place - I would therefore suggest that the subset of that type be automatically determined and inferred, resulting in a function that doesn't demand values it can't actually either see or use.
π» Use Cases
In general, there is no point in passing arguments to functions that can't even see them - destructuring hides the original object, and the function only receives the individual properties; since it can't access the object itself, it ultimately doesn't matter what the type of that argument is.
If you had to, for example, mock the arguments to a function for testing, this makes that much easier to - in the example, you can simply call
f({ a: 1 })
and that would be valid, without have to resort toas any
, or explicitly type-hinting withPick
, neither of which make much sense.β Caveat
This being JavaScript, you can of course access the object using
arguments[0]
- so the type of this object could in fact matter.However, the type of
arguments[0]
is alwaysany
, and so, in that case, there's no type safety either way; if the rest of the type is important, you probably aren't (and likely shouldn't be) destructuring the argument in the first place.So this seems unlikely to affect anything other than things like currying and higher-order functions, where you wouldn't be destructing anyway - and so, it seems unlikely this will cause any adverse effects in practice.
What do you think?
The text was updated successfully, but these errors were encountered: