I have a function:
export default ({
input: { name, onChange, value, ...restInput },
meta,
...rest
}) => (
...
);
Given that 'name' is a string, 'onChange' is a function, 'value' is a string, 'meta' is an object, how can I add types to those parameters? My best guess would be like so:
export default ({
input: { (name: String), (onChange: function), (value: String), ...restInput },
(meta: Object),
...rest
}) => (
...
);
But it seems to have syntax errors. And even more I have no idea how to add types to rest parameters.
To add type declarations to destructured parameters you need to declare the type of the containing object.
From the typescript documentation:
A PSA about deeply nested destructuring:
Destructuring function parameters
In a function, this is how you would declare types for destructured parameters:
This looks pretty bad on a longer example though:
Looks pretty bad, so the best you can do here is to declare an interface for your parameters and use that instead of inline types:
Playground
Article with much more
Rest parameters
For the rest parameters, depending on what you expect these types to be, you can use a index signature:
This will give
...rest
a type of{[key: string]: object}
for example.Rest parameter playground