0

I got this error while using typescript.

Type 'string' is not assignable to type 'never'.

It works fine on javascript.

const [gifList, setGifList] = useState([])

const sendGif = async () => {
    if (inputValue.length > 0) {
        console.log('Gif link:', inputValue)
        setGifList([...gifList, inputValue])
        setInputValue('')
    } else {
        console.log('Empty input. Try again')
    }
}

Could you please tell me how to properly declare a type in Typescript.

1 Answer 1

4
const [gifList, setGifList] = useState([])

Since you havn't defined the type here, typescript makes its best guess. It sees that you set the state to an array, but it doesn't know what's supposed to be inside that array, so the best it can do is to treat the state as never[].

useState is a generic, so you can use that to specify the type of state:

const [gifList, setGifList] = useState<string[]>([])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.