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

Array<A|B> should be compatible with Array<A> #3425

Closed
aldendaniels opened this Issue Feb 23, 2017 · 8 comments

Comments

Projects
None yet
3 participants
@aldendaniels

aldendaniels commented Feb 23, 2017

This:

declare function func(things: Array<string | number>): void;
declare var value: string[];
func(value);

Gives:

1: declare function func(things: Array<string | number>): void;
                                                ^ number. This type is incompatible with
2: declare var value: string[];
                      ^ string
@aldendaniels

This comment has been minimized.

Show comment
Hide comment
@aldendaniels

aldendaniels Feb 23, 2017

Perhaps related: #2507

aldendaniels commented Feb 23, 2017

Perhaps related: #2507

@omerzach

This comment has been minimized.

Show comment
Hide comment
@omerzach

omerzach Feb 23, 2017

Contributor

Arrays are invariant: T being a subtype of U does not mean Array<T> is a subtype of Array<U>. Otherwise you could push a number onto things inside of func and there would be a number in value, which has type Array<string>.

Contributor

omerzach commented Feb 23, 2017

Arrays are invariant: T being a subtype of U does not mean Array<T> is a subtype of Array<U>. Otherwise you could push a number onto things inside of func and there would be a number in value, which has type Array<string>.

@jesseschalken

This comment has been minimized.

Show comment
Hide comment
@jesseschalken

jesseschalken Feb 23, 2017

Contributor

Can't because func(value) could add a number to value.

function func(things: Array<string | number>): void {
  things.push(1);
}
var value: string[] = ['f'];
func(value);
console.log(typeof value[1]); // a number!
Contributor

jesseschalken commented Feb 23, 2017

Can't because func(value) could add a number to value.

function func(things: Array<string | number>): void {
  things.push(1);
}
var value: string[] = ['f'];
func(value);
console.log(typeof value[1]); // a number!
@aldendaniels

This comment has been minimized.

Show comment
Hide comment
@aldendaniels

aldendaniels Feb 23, 2017

@jesseschalken @omerzach Wow, thanks for the quick follow up! Is there a way to make flow allow this if the function doesn't mutate the array - e.g. using a readonly array?

aldendaniels commented Feb 23, 2017

@jesseschalken @omerzach Wow, thanks for the quick follow up! Is there a way to make flow allow this if the function doesn't mutate the array - e.g. using a readonly array?

@jesseschalken

This comment has been minimized.

Show comment
Hide comment
@jesseschalken

jesseschalken Feb 23, 2017

Contributor

You can use func<T:string|number>(things: Array<T>): void or, of any Iterable will do, func(things: Iterable<string|number>): void (Iterable<T> is covariant in T).

Contributor

jesseschalken commented Feb 23, 2017

You can use func<T:string|number>(things: Array<T>): void or, of any Iterable will do, func(things: Iterable<string|number>): void (Iterable<T> is covariant in T).

@jesseschalken

This comment has been minimized.

Show comment
Hide comment
@jesseschalken

jesseschalken Feb 23, 2017

Contributor

See also #2333

Contributor

jesseschalken commented Feb 23, 2017

See also #2333

@aldendaniels

This comment has been minimized.

Show comment
Hide comment
@aldendaniels

aldendaniels Feb 23, 2017

@jesseschalken Sure enough! func(things: $ReadOnlyArray<T>) also appears to work

aldendaniels commented Feb 23, 2017

@jesseschalken Sure enough! func(things: $ReadOnlyArray<T>) also appears to work

@aldendaniels

This comment has been minimized.

Show comment
Hide comment
@aldendaniels

aldendaniels Feb 23, 2017

Closing in favor of #2333

aldendaniels commented Feb 23, 2017

Closing in favor of #2333

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment