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
lodash.get returns any? #3771
Comments
You have to have an |
And of course you need an |
Easier to use optional chaining which is supported by flow very well. |
I have a function that hunts for the path of a given type in a GraphQL result and returns that path as an array, which I can then use with |
@sospedra I realized as well, if you pass a path of type As @TrySound points out, optional chaining works much better for this and as it becomes more widespread people will stop using |
The overload I posted works perfectly: get<
+O: { +[string]: mixed },
S1: $Keys<O>,
S2: $Keys<$ElementType<O, S1>>,
E: mixed
>(
object: O,
path: [S1, S2],
defaultValue?: ?E
): $ElementType<$ElementType<O, S1>, S2> | E; And infers properly everything. The only limitation from flow (and TS) is |
This is what I have currently. get<+O: { +[string]: mixed }, S: $Keys<O>>(
object: O,
path: S
): $ElementType<O, S>;
get<+O: { +[string]: mixed }, S: $Keys<O>, E: $NonMaybeType<mixed>>(
object: O,
path: S,
defaultValue?: E
): $NonMaybeType<$ElementType<O, S> | E>;
get<
+O: { +[string]: mixed },
S1: $Keys<O>,
S2: $Keys<$ElementType<O, S1>>,
E: mixed
>(
object: O,
path: [S1, S2],
defaultValue?: ?E
): $ElementType<$ElementType<O, S1>, S2> | E;
get<
+O: { +[string]: mixed },
S1: $Keys<O>,
S2: $Keys<$ElementType<O, S1>>,
S3: $Keys<$ElementType<$ElementType<O, S1>, S2>>,
E: mixed
>(
object: O,
path: [S1, S2, S3],
defaultValue?: ?E
): $ElementType<$ElementType<$ElementType<O, S1>, S2>, S3> | E;
get<A: $ReadOnlyArray<mixed> | RegExp$matchResult | null, I: number>(
object: A,
path: I
): mixed;
get<O: void | null, E: $NonMaybeType<mixed>>(
object: O,
path: mixed,
defaultValue: E
): E; |
@sospedra what I'm saying is it will infer const path: string[] = []
path.push('foo')
path.push('bar')
get(obj, path) |
This is a question and maybe a suggestion
As far as I know,
get
returningany
is basically useless. And all uses ofany
discouraged byflow
maintainers.Why not use
$ElementType
to - at least - validate the possible types?straight from flow docs
I'd love to have the power of TS here but as far as I'm concerned we cannot infer as well as they do.
I'm using this currently
Works only for
get
withobject
andpath
asstring
or 2-length arrays. But it's only mather to overload this method to support the array inobject
and N-length paths.The question: What am I missing? 🤔
The text was updated successfully, but these errors were encountered: