1

I have the following type.

type Team = 'liverpool' | 'manUtd' | 'arsenal' | null;

I then have the following object.

const teams: Record<Team, JSX.Element> = {
  liverpool: <Liverpool />,
  manUtd: <ManU />,
  arsenal: <Arsenal />,
};

If I use teams as the first part of the Record I see the following error:

Type 'Team' does not satisfy the constraint 'string | number | symbol'.
  Type 'null' is not assignable to type 'string | number | symbol'.ts(2344)

If I use Record<string, JSX.Element> it works fine.

2
  • null cannot be used as a key. Why do you need null in your type definition? Commented Oct 11, 2019 at 8:15
  • thanks @georg elsewhere in the app it can be null, as it does not always exist :-) Commented Oct 11, 2019 at 8:16

2 Answers 2

2

Record<NonNullable<Team>, JSX.Element>

Let me know if this helps. this error appears because null is not a valid javascript object key type.

Sign up to request clarification or add additional context in comments.

1 Comment

yeah, not a clue either. I actually thought my upvote wasn't working, and that there was a bug with SO :-D I will give you the green tick when I can anyway, cheers
1

The type for Record is Record<K extends string | number | symbol, T>.

So the first type you provide to the Record must be either string, number or symbol. You can't assign null to it so your Team type does not satisfy the constraint. You should remove null from your Team type and it'll work like a charm.

1 Comment

Indeed, team: null doesn't make any sense.

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.