Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[@types/lodash]: _.get types look partially broken in TS3.6 #38065

nhardy opened this issue Sep 2, 2019 · 1 comment


Copy link

commented Sep 2, 2019

It looks like TS3.6 has partially broken the type for get when object can be null or undefined and a defaultValue is specified. When this happens, instead of inferring the type of TKey correctly as the single value that is specified, it infers the value as all possible keys of object.

import { get } from "lodash";

const object = Math.random() > 0.5
  ? { key: "value" otherKey: 1 }
  : undefined;
// This line will works in TS3.5, but errors in TS3.6 because `TKey` is inferred as `"key" | "otherKey"` instead of just `"key"`
const result: string = get(object, ["key"], "default");

If I change the line to const result: string = get(object, ["key"] as const, "default"); and update the typings to accept readonly [TKey], readonly [TKey1, TKey2], etc. this also resolves the issue for TS3.6, but I don't think adding as const should be required.

@nhardy nhardy changed the title lodash.get types look partially broken in TS3.6 [@types/lodash]: _.get types look partially broken in TS3.6 Sep 2, 2019


This comment has been minimized.

Copy link

commented Sep 2, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
2 participants
You can’t perform that action at this time.