flown icon indicating copy to clipboard operation
flown copied to clipboard

`Arguments` appears to be lossy when using spread syntax

Open magicmark opened this issue 5 years ago • 3 comments

First of - thanks @lttb so much for this library! It's a huge help!

I'm running into the following issue best demonstrated by a small repro:

type GetUserInfoArgs = {| user_id: number |};
type GetUserInfo = (GetUserInfoArgs) => { first_name: string, last_name: string };
type DerivedArg = $ElementType<Arguments<GetUserInfo>, 0>; // should be equivalent to `GetUserInfoArgs`?

/**
 * Using Arguments<GetUserInfo>:
 * - `a` fails type checking as expected
 * - `b` does not fail type checking :( 
 */
const a: DerivedArg = { foo: 'bar' };
const b: {| ...DerivedArg |} = { foo: 'bar' };

/**
 * Without using Arguments<GetUserInfo>:
 * - `c` fails type checking as expected
 * - `d` fails type checking as expected 
 */
const c: GetUserInfoArgs = { foo: 'bar' };
const d: {| ...GetUserInfoArgs |} = { foo: 'bar' };

Link to full repro in tryflow

Perhaps I'm missing something, but I'd expect flow to complain about b?

Thanks!

magicmark avatar Sep 09 '19 00:09 magicmark

Paring it back a bit more:

type GetUserInfoArgs = {| user_id: number |};
type GetUserInfo = (GetUserInfoArgs) => { first_name: string, last_name: string };

// Similar to how `Arguments` works - produces the same error
type ExtractArg = <A, F: (A) => any>(F) => A & A
type DerivedArg = $Call<ExtractArg, GetUserInfo>

/**
 * Using Arguments<GetUserInfo>:
 * - `a` fails type checking as expected
 * - `b` does not fail type checking :( 
 */
const a: DerivedArg = { foo: 'bar' };
const b: {| ...DerivedArg |} = { foo: 'bar' };

/**
 * Without using Arguments<GetUserInfo>:
 * - `c` fails type checking as expected
 * - `d` fails type checking as expected 
 */
const c: GetUserInfoArgs = { foo: 'bar' };
const d: {| ...GetUserInfoArgs |} = { foo: 'bar' };

magicmark avatar Sep 09 '19 00:09 magicmark

Hmm, related? https://github.com/facebook/flow/issues/7709

magicmark avatar Sep 09 '19 02:09 magicmark

Hi @magicmark, thank you very much for the issue!

I'll try to check it asap, I hope to have a chance to look at this tomorrow

lttb avatar Sep 09 '19 18:09 lttb