0

There are two errors I'm getting when trying to learn TypeScript.

Type 'string | null | undefined' is not assignable to type 'string | RegExp | QuerySelector<string | RegExp> | undefined'.

Type 'null' is not assignable to type 'string | RegExp | QuerySelector<string | RegExp> | undefined'.

This line in particular:

const warrior = await Warrior.findOne({ warriorname })

This comes from the code

async (warriorname) => {
    const warrior = await Warrior.findOne({ warriorname })
    return !warrior
}

How can I fix this?

0

1 Answer 1

3

From the error it seems that Warrior.findOne would happily accept something like this:

const warrior = await Warrior.findOne({ warriorname: 'some string' })

or this:

const warrior = await Warrior.findOne({ warriorname: undefined })

or even this:

const warrior = await Warrior.findOne({ warriorname: /some regex/ })

but not this:

const warrior = await Warrior.findOne({ warriorname: null })

Having said that, your warriorname variable (again, judging only by the error) looks like it can be either a string (which would work) or undefined (which would work) or null (which would not work).

You can fix this by handling the null case separately, like so:

if (warriorname === null) {
   return some error;
}

const warrior = await Warrior.findOne({ warriorname })
...
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.