Unnecessary useState Type Has Fix
Description
React's useState is a Hook that lets you add a state variable to your component. The type annotation of useState's generic type argument, for example useState<number>(123), is unnecessary if TypeScript can infer the type of the state variable from the initial value.
We can usually skip annotating if the generic type argument is a single primitive type like number, string or boolean.
Pattern
bash
ast-grep -p 'useState<number>($A)' -r 'useState($A)' -l tsxbash
ast-grep -p 'useState<string>($A)' -r 'useState($A)'bash
ast-grep -p 'useState<boolean>($A)' -r 'useState($A)'Example
ts
function Component() {
const [name, setName] = useState<string>('React')
}Diff
ts
function Component() {
const [name, setName] = useState<string>('React')
const [name, setName] = useState('React')
}