score:3
Accepted answer
you can keep track of the argument types if you add an extra generic parameter a extends unknown[]
to both useasync
& useasync
, and type each occurrence of ...args
with it.
the result would look something like this (i had to tweak a few null
types, possibly because the code wasn't compiled with strictnullchecks
):
type useasync<t, a extends unknown[]> =
[{ result: t | null; loading: boolean; error: string }, (...args: a) => void];
export const useasync = <t, a extends unknown[]>(asyncfn: (...args: a) => promise<t>): useasync<t,a> => {
const [loading, setloading] = usestate(false);
const [result, setresult] = usestate<t | null>(null);
const [error, seterror] = usestate<any>(null);
const callback = usecallback(
async (...args: a) => {
try {
setloading(true);
const result = await asyncfn(...args);
setresult(result);
} catch (e) {
seterror(e);
} finally {
setloading(false);
}
},
[asyncfn]
);
return [{ result, loading, error }, callback];
};
when applied to a test function, the type of postorder
has the actual argument types:
type ineworderformdata = {}
const [{ result, loading, error }, postorder] =
useasync(async (data: ineworderformdata, arg2: symbol) => 42);
type test = typeof postorder
// inferred type: (data: ineworderformdata, arg2: symbol) => void
Source: stackoverflow.com
Related Query
- Typescript: Infer arguments of a callback function passed as a parameter to another function
- How to pass arguments to callback in React + Typescript without new function creation?
- How to let typescript infer the indirect function parameter type?
- Define function arguments as object in Typescript
- React: using state in a passed callback function to a class constructor doesnt use latest version of state
- How to declare a function type as a parameter in TypeScript
- How to create generic typescript function that binds arguments for storybook control
- Pass parameter from a function to this.setState callback function
- useEffect cleanup function Typescript error: not assignable to parameter of type 'EffectCallback'
- How to pass multiple arguments in a function passed through Props
- Optionally pass parameter to function being passed around
- Typescript React: generic-based callback event handler function as prop to component
- Withrouter Error using function component and typescript jsx.element' is not assignable to parameter of type routecomponentprops
- React.js useState function passed as a parameter doesn't rerender its component
- Create a custom onChange function as a parameter for a component in react, which takes 2 custom arguments
- Typescript Interfaces - Implement function with narrowed union type parameter
- How to add two arguments using Typescript Generics in Arrow Function
- How do I bind context to event handler so event and a few more arguments passed to handler function
- Typescript with React, infer type of array passed as prop
- In Typescript how do you assure a function parameter is one of a number of defined enums?
- React component throwing TypeScript error for function passed in as prop
- Passing a callback function with arguments coming from different components as a prop
- Is there a way infer the return value type of a callback instead of pass the generic parameter
- Cannot satisfy wrapper parameter for react testing library render function using TypeScript
- Function passed as parameter to custom hooks is not taking new value of variables
- Function passed for for child component don't work in ReactJS with Typescript
- Set default value of property of object passed on parameter to function
- Awesome Typescript Loader throwing error about too many function arguments
- Getting parameter types for function of Type T with arguments type U of T
- TypeScript Type for function arguments not erroring?
More Query from same tag
- rule validation not triggered on input field when value is changed from React state
- How to set state in react whith the content at a certain index from an array
- Why usePrevious returns undefined on first render - react hooks?
- Uncaught TypeError: this.state.data.map is not a function
- React JS How to make some checkboxes checked, based on an array of checked values
- JavaScript: var {left, ...props} = this.props;
- React Material UI Autocmplete with React-window rerenders at top of list
- Grid and Ant Design
- Looping through certain elements in React
- How to transfer data between pages in Reactjs
- How can I change inline style to css using Material-ui in React?
- Which renders first child or parent in reactjs
- Cannot render the react component
- Which face is further in parallel projections?
- Why I am getting: No `render` method found on the returned component instance: you may have forgotten to define `render`
- How do i fix items not being shown on screen from mongo db?
- CORS issue performing api calls in react/redux application
- How to get dispatch redux
- React-Apollo: Specifying option fetchPolicy: "network-only" using withApollo doesn't work
- display one image at a time from fetched data from json
- display icon based on file type
- Input & Label Do Not Vertically Align - Different Element Widths
- How to loop through string and check if there are unmatched letters
- Is it possible to make android webview based app less sensitive in interpreting taps as small drags?
- MUI v5: Do I need to install @emotion/react or @emotion/styled to use sx prop?
- How to apply a pattern to a TextField to only allow specific characters
- why I am getting error in the code in nextjs script tag
- Trying to convert HTML code that I get from server to Image , but gives me an error, in ReactJS
- Simulate change does not work for formik's select elements
- props.onChange(e.target.value) returns an object instead of a value in textField in material ui