birdofpreyru/react-global-state

`useGlobalState()` type deduction works wrong when `[]` is used in path?

Closed this issue · 1 comments

Double-check this scenario:

type TA = {
  [key: string]: string | undefined;
};

type StateT = {
  a: { [key: string]: TA | undefined };
};

let keyA: string;
let keyB: string;

const [value] = useGlobalState(`a['${keyA}']['${keyB}']`);

Here the expectation for value type to be deduced as string | undefined; but it actually resolves to TA | undefined (seen in a real project on similar, but a bit more complex code).

And it seems to work correctly when re-written as

const [value] = useGlobalState(`a.${keyA}.${keyB}`);

This is a bug in Lodash's GetFieldType<>, which will be fixed by PR DefinitelyTyped/DefinitelyTyped#69696.