Bohdan Shulha
Bohdan Shulha

Reputation: 74

Typescript Record type with string keys infers invalid type for it's values

I'm having a simple Record with string keys that defined like this:

type Data = Record<string, string>;

I'm accessing values in a really simple way:

const data: Data = {};

const entry = data['entry'];

Is there a way to tell Typescript to treat these values as string | undefined and not simply string, except defining maps with Record<string, string | undefined>?

Strict mode is enabled, so I didn't expect to see the behavior in the real code.

Playground

Upvotes: 0

Views: 711

Answers (1)

brunnerh
brunnerh

Reputation: 184416

For that you need to set the flag noUncheckedIndexedAccess, which is not part of strict because it was deemed to be potentially too disruptive.

[Playground link with flag]

Upvotes: 1

Related Questions