score:0
it's not weird at all - in given syntax (function loaddata<t, u = tresponse<t>>() {...}
) the u
type can be "anything" - so it's not possible for typescript to know what may it be during the runtime as only u
sub-types are assignable to it, and the { type: 'error', error: 'error message'}
is not a sub-type of u
it won't get any better if you will change the definition to function loaddata<t, u extends tresponse<t> = tresponse<t>>() {...}
, because situation is the same, the u
type just got narrower though
in situation like this one you can't use generics for internal values (so the correct definition would be just function loaddata<t>() {...}
), unless you somehow get the u
type from somewhere, eg.
function loaddata<t, u = tresponse<t>>(loader: () => u) {
const [data, setdata] = usestate<u | null>(null)
setdata(loader());
}
loaddata<number>(() => ({ type: 'error', error: 'it works, lol' }))
Source: stackoverflow.com
Related Query
- Typescript error when use react useState hook with generics
- How to use useState hook in React with typescript correctly?
- Typescript error on react useState hook when updating typescript version
- Set types on useState React Hook with TypeScript
- How to use callback with useState hook in react
- What typescript type do I use with useRef() hook when setting current manually?
- Cannot find namespace 'ctx' error when creating Context with react - typescript
- Why does react hook throw the act error when used with fetch api?
- How to use React Context with useState hook to share state from different components?
- When to use private/protected methods in Typescript with React
- React Custom Hook with Typescript Type error "Property 'x' does not exist on type 'interface | (({ target }: any) => void)'.ts(2339)"
- `Invalid mapping` error trying to use babel require hook and polyfill with react jsx transpile
- How to use React Hook Form with Typescript (Input Component)
- use PrevState in useState hook in React typescript
- How do I fix a React TypeScript error with the return value of useHotkeys hook not matching the type of the div element ref prop
- React Context with useState in TypeScript requires initialState even when it's pointless?
- React Redux Typescript error with AnyAction when implementing Redux-Persist
- React useState hook - when to use previous state when updating state?
- Typescript error got undefined with useState hook
- Error "SyntaxError: Cannot use import statement outside a module" when deploying React app with Netlify Functions
- Object is of type unknown error when using Typescript with React
- When setting state with React's use state hook using it's prev (or initial) value throws an error Type Error 0 is not a function
- How to define TypeScript types when used with React useState and the previous state inside a state updating function?
- Typescript error showing up when update method for state element by using React useState
- Error when Using Custom React Hook in a Multi-Module Project for Tests with Jest
- Invalid Hook Call Error when using react useState hook inside firebase app
- Getting Error When Trying to Render Page with React Hook
- React Typescript ERROR when running npm run build with webpack
- Typescript throws an explicit function return type error when using Suspense with React Router
- How do I fix a uncaught in promise error in react with typescript when doing a axios post request?
More Query from same tag
- Data unavailable before render method is executed - React Native
- TypeError: Cannot read property 'map' of undefined, issue in passing props
- property missing in returned json response / javascript
- Put request with Axios | React, Redux
- React multiple select form saving only one option to array
- How can I update a json object inside of an array?
- I am getting a 404 error on my new Reactjs app, is it looking for a main.js file?
- Why do I have two vertical scrollbars after switching from ReactJS to NextJS?
- Button onClick triggered when init in React application
- OnBlur closing react component if clicked inside
- In react-testing-library is render supossed to be called only once?
- React Js: export problems '(possible exports: default)'
- search list for keywords in react
- React build run on server using pm2 in windows
- TypeError: projects.map is not a function (React)
- Is it possible to configure IIS (specifically, in Azure) to work with HistoryLocation?
- ReactJS dynamic button bind
- Embedded expression rendering as array instead of individual elements
- React - parent passing data to child becomes undefined
- How to wait for two async functions to end?
- How to get values from same input type from multiple fields in react?
- react component detect changes only after a refresh
- Next JS with react Bootsrap
- Instantiate hook context using generics inside of a functional component
- React DatePicker range with one input Style
- React Navbar onClick redirect component
- React onChange event will not fire on custom Input
- How can I dispatch something with React / Redux?
- React.memo performance is worse than with React.PureComponent
- React Redux app structure multiple roles