score:3
If you don't specify a type parameter to useState
the type of the state will be inferred from the value passed to useState
. Since you pass in null
, the type of the state will be null
, meaning that it can have no other value except null
.
You can specify a wider type for the state as a type parameter:
const [intervalData, setIntervalData]:[null | NodeJS.Timeout, (interval: null | NodeJS.Timeout) => void] = useState<null | NodeJS.Timeout>(null);
Note: I would drop the type annotation from the const
altogether, but I'm guessing that might have been added just for the example in the question.
Source: stackoverflow.com
Related Query
- getting error : Argument of type '() => () => boolean' is not assignable to parameter of type 'EffectCallback'
- Getting an error Argument of type 'unknown' is not assignable to parameter of type 'Error | null'
- getting error : void' is not assignable to type 'FC<IPickWinnerProps>' in react js
- getting error :Type '[null, Dispatch<SetStateAction<null>>]' is not assignable to type in typescript react
- Getting a Typescript error saying, some type is not assignable to parameter of type 'ReducerWithoutAction<any>'?
- Getting error "Argument of type 'typeof Header' is not assignable to parameter of type 'ComponentType...' or 'StatelesComponent'..."
- getting error type any is not assignable to type never
- CombineReducers with Typescript returns error "Argument of type is not assignable to parameter of type 'ReducersMapObject'"
- Why am I getting the error message "Property 'then' does not exist on type 'AsyncThunkAction'"?
- React Typescript error in TextArea : Type 'string' is not assignable to type number
- React Hook Form Error - Type 'UseFormRegister<FormData>' is not assignable to type 'LegacyRef<HTMLInputElement> | undefined'
- TS2322 - false | Element is not assignable to type ReactElement. Error does not consistently appear in app?
- Creating reusable Button Component with React and Typescript with not assignable type error
- TypeScript/React. ERROR Type '{ key: number; }' is not assignable to type ILink (SFC Component)
- Getting a "Uncaught ReferenceError: type is not defined" error in React / Redux?
- How to fix the type error Type '(string | Element) [] is not assignable to type 'string | Element | undefined' using react and typescript?
- Typescript error 'argument of type is not assignable to parameter of type'
- TypeScript error : Type '.....' is not assignable to type 'IntrinsicAttributes & Number'
- Promise.all error - Type '(Video | undefined)[]' is not assignable to type 'Video[]'
- Using state and includes in React gives Argument of type 'number' is not assignable to parameter of type 'never' error
- Withrouter Error using function component and typescript jsx.element' is not assignable to parameter of type routecomponentprops
- Getting 'is not assignable to type' error on react component, how can I understand it better?
- React Typescript error Type '{ id: number; title: string; function(): void; }' is not assignable to type 'null'
- Typescript: Type 'null' is not assignable to type error
- Cryptic Typescript compiler error using React: 'ComponentClass<StyledComponentProps<{}>>' is not assignable to type 'typeof MyComponent'
- Error argument of type 'Item' is not assignable to parameter of type 'string'. TS2345
- Type '(isOpen: boolean) => void' is not assignable to type 'boolean'. Error when using Modal in reactjs
- React router dom type error / FC{} | ReactNode is not assignable to type ReactNode
- Initial value of function in useRef throws an error cause "Argument of type '() => never' is not assignable to parameter of type 'T'."
- react typescript error 'Type '{ ... }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<...>
More Query from same tag
- Cast to ObjectId failed for value "undefined" (type string) at path "_id" for model "Car"
- Want to create Gmail like Swipeable Card using React JS (Not React Native)
- How do I create a Draft.js editor with content from a rest API?
- React - Is there is a difference between TransitionGroup / ReactCSSTransitionGroup / CSSTransitionGroup
- What is the correct way to pass through generics in this instance?
- Make a Confirmation Modal using React & Redux
- Videos In React Going Fullscreen
- ReactJS - Error while loading data from json file
- How to create a string from a javascript array
- Next.js Head - You have included the Google Maps JavaScript API multiple times on this page
- React too many rending with useState
- Rendering props.children with React
- Passing variables for media queries and rendering css in styled components?
- Draft.js inline style not working without text selection
- React.js - componentWillReceiveProps being hit twice
- In React hooks, how do I refer to a generic attribute of an object in my state I want to change?
- dispatch is not defined inside async action creators
- Basic React/redux testing with mocha/chai
- React props - set isRequired on a prop if another prop is null / empty
- React router NavLink activeClassName does not work with redux and styled components
- Highcharts React update animations with datetime axis
- React Monorepo yarn workspaces + typescript + absolute imports
- React Unexpected token 'export' when implementing serviceworker
- "ReferenceError: path is not defined" in my Gatsby project
- Display responsive header in single line using Bootstrap (Reactstrap)
- React Passing Component Function - cannot use in function
- React formatting issue
- How to fix map typerror when a promise object is returned before the array?
- Unexpected behaviour using React useEffect hook
- How to recognize env variables in typescript when using vite?