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

Unnecessary widening of function argument's type #19682

Closed
timocov opened this issue Nov 2, 2017 · 5 comments · Fixed by #20467
Closed

Unnecessary widening of function argument's type #19682

timocov opened this issue Nov 2, 2017 · 5 comments · Fixed by #20467
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@timocov
Copy link
Contributor

timocov commented Nov 2, 2017

TypeScript Version: 2.6.1 (2.7.0-dev.20171102)

Code

type Nominal<Kind extends string, Type> = Type & {
    [Symbol.species]: Kind;
};

type A = Nominal<'A', string>;

declare const a: Set<A>;
declare const b: Set<A>;

const c1 = Array.from(a).concat(Array.from(b)); // Argument of type 'string[]' is not assignable to parameter of type 'Nominal<"A", string> | ReadonlyArray<Nominal<"A", string>>'.
const c2 = Array.from(a).concat(Array.from<A>(b)); // OK

const aa = Array.from(a);
const bb = Array.from(b);
const cc = aa.concat(bb); // OK

Expected behavior:
No errors.

Actual behavior:
With Array.from(a).concat(Array.from(b)) we have an error Argument of type 'string[]' is not assignable to parameter of type 'Nominal<"A", string> | ReadonlyArray<Nominal<"A", string>>'.

In 2.5.3 it works fine.

@ghost
Copy link

ghost commented Nov 2, 2017

A smaller repro:

type A = string & { n: number };
declare function from<T, U = T>(iterable: Iterable<T>, mapfn?: (v: T, k: number) => U): U[];
declare function concat<T>(a: ReadonlyArray<T>, b: ReadonlyArray<T>): T[];

declare const iterA: Iterable<A>;
const a: A[] = from(iterA); // OK
const cat: A[] = concat(a, from(iterA)); // Error

It would work if you remove U and mapfn from from, but that shouldn't be necessary.
We've had some similar problems with Array and ReadonlyArray in the past.

@ghost ghost added the Bug A bug in TypeScript label Nov 2, 2017
@mhegazy mhegazy added Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Help Wanted You can do this labels Nov 2, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Nov 2, 2017

We need to split the from function into two overloads i would say.

@mhegazy mhegazy added this to the Community milestone Nov 2, 2017
@ghost
Copy link

ghost commented Nov 2, 2017

@mhegazy Should that really be necessary? We usually recommend to unify signatures like that.

@mhegazy
Copy link
Contributor

mhegazy commented Nov 2, 2017

Should that really be necessary? We usually recommend to unify signatures like that.

The U here is just like map, you want to allow a function to transform the input to a different type and infer that independently.

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Nov 2, 2017
@mhegazy mhegazy modified the milestones: Community, TypeScript 2.7 Nov 2, 2017
@ahejlsberg
Copy link
Member

This one is actually caused by a subtle issue introduced by #18363. I have a fix that I will put up shortly. Meanwhile, the change to lib.d.ts is probably fine, but strictly speaking isn't needed.

@ahejlsberg ahejlsberg reopened this Nov 2, 2017
@ahejlsberg ahejlsberg self-assigned this Nov 2, 2017
@ahejlsberg ahejlsberg removed Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Help Wanted You can do this labels Nov 2, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants