Skip to content

Make 'pop' the first method that uses 'T' in 'Array' - #25565

Merged
Daniel Rosenwasser (DanielRosenwasser) merged 4 commits into
masterfrom
popFirst
Jul 16, 2018
Merged

Make 'pop' the first method that uses 'T' in 'Array'#25565
Daniel Rosenwasser (DanielRosenwasser) merged 4 commits into
masterfrom
popFirst

Conversation

@DanielRosenwasser

@DanielRosenwasserDaniel Rosenwasser (DanielRosenwasser) commented Jul 10, 2018

Copy link
Copy Markdown
Member

Chatted with Mohamed Hegazy (@mhegazy) about this, but I'd like to get your thoughts on this Anders Hejlsberg (@ahejlsberg) since you sent out #436.

When elaborating errors on an Array<Foo> to an Array<Bar>, diving into a structural comparison is often dizzying for the user since the first elaboration occurs on push.

Since push uses T contravariantly, we'll end up flipping the direction which is much more difficult for users to parse. Furthermore, in cases where the covariant check would provide better error messages and error message spans (such as excess property checking spans, and picking better branches in unions), we would end up doing significantly worse.

As an example from Cliff Koh (@cliffkoh) and David Zearing (@dzearing), the following

typeIStyle=IStyleBase|IStyleArray;interfaceIStyleArrayextendsArray<IStyle>{}interfaceIStyleBase{foo: string;}constblah: IStyle=[[[{foo: 'asdf',jj: 1// intentional error}]]];

Provides an error on blah:

const blah: IStyle = [
~~~~
[ts] Type '{ foo: string; jj: number; }[][][]' is not assignable to type 'IStyle'.
Type '{ foo: string; jj: number; }[][][]' is not assignable to type 'IStyleArray'.
Types of property 'push' are incompatible.
Type '(...items: { foo: string; jj: number; }[][][]) => number' is not assignable to type '(...items: IStyle[]) => number'.
Types of parameters 'items' and 'items' are incompatible.
Type 'IStyle' is not assignable to type '{ foo: string; jj: number; }[][]'.
Type 'IStyleBase' is not assignable to type '{ foo: string; jj: number; }[][]'.
Property 'length' is missing in type 'IStyleBase'.

Whereas with this change, we provide an error on jj (the excess property):

 jj: 1 // intentional error
~~
[ts] Type '{ foo: string; jj: number; }[][][]' is not assignable to type 'IStyle'.
Type '{ foo: string; jj: number; }[][][]' is not assignable to type 'IStyleArray'.
Types of property 'pop' are incompatible.
Type '() => { foo: string; jj: number; }[][]' is not assignable to type '() => IStyle'.
Type '{ foo: string; jj: number; }[][]' is not assignable to type 'IStyle'.
Type '{ foo: string; jj: number; }[][]' is not assignable to type 'IStyleArray'.
Types of property 'pop' are incompatible.
Type '() => { foo: string; jj: number; }[]' is not assignable to type '() => IStyle'.
Type '{ foo: string; jj: number; }[]' is not assignable to type 'IStyle'.
Type '{ foo: string; jj: number; }[]' is not assignable to type 'IStyleArray'.
Types of property 'pop' are incompatible.
Type '() => { foo: string; jj: number; }' is not assignable to type '() => IStyle'.
Type '{ foo: string; jj: number; }' is not assignable to type 'IStyle'.
Object literal may only specify known properties, and 'jj' does not exist in type 'IStyle'.

This is horrifyingly longer, but it gives the right span and gets to the root cause at the end.

@DanielRosenwasserDaniel Rosenwasser (DanielRosenwasser) changed the title Move 'pop' to to the first method using 'T' in 'Array'Move 'pop' to the first method using 'T' in 'Array'Jul 12, 2018
@DanielRosenwasserDaniel Rosenwasser (DanielRosenwasser) changed the title Move 'pop' to the first method using 'T' in 'Array'Make 'pop' the first method that uses 'T' in 'Array'Jul 12, 2018
@DanielRosenwasser

Copy link
Copy Markdown
MemberAuthor

Really the RWC baseline changes are minimal, and aren't impacted at all. I'm just seeing reports of missing pop instead of push:

 Argument of type 'Uint8ClampedArray' is not assignable to parameter of type 'any[]'.
+ Property 'pop' is missing in type 'Uint8ClampedArray'.- Property 'push' is missing in type 'Uint8ClampedArray'.

@mohsen1

Copy link
Copy Markdown
Contributor

Can you improve those errors for Arrays even further. I got confused with code like this:

interfaceValues{textValues: {values: string[]}}constitems=[{name: 'a'},{name: 'b'}]varvalues: Values={textValues: items.map(i=>i.name)}
Type 'string[]' is not assignable to type '{ values: string[]; }'.
Types of property 'values' are incompatible.
Type '() => IterableIterator<string>' is not assignable to type 'string[]'.
Property 'pop' is missing in type '() => IterableIterator<string>'

Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@DanielRosenwasser@mohsen1@sheetalkamat