Skip to content

Commit

Permalink
Merge pull request #25565 from Microsoft/popFirst
Browse files Browse the repository at this point in the history
Make 'pop' the first method that uses 'T' in 'Array'
  • Loading branch information
DanielRosenwasser authored Jul 16, 2018
2 parents afdd47c + 751bceb commit 93ab352
Show file tree
Hide file tree
Showing 14 changed files with 186 additions and 35 deletions.
8 changes: 4 additions & 4 deletions src/lib/es5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1125,15 +1125,15 @@ interface Array<T> {
* Returns a string representation of an array. The elements are converted to string using their toLocalString methods.
*/
toLocaleString(): string;
/**
* Removes the last element from an array and returns it.
*/
pop(): T | undefined;
/**
* Appends new elements to an array, and returns the new length of the array.
* @param items New elements of the Array.
*/
push(...items: T[]): number;
/**
* Removes the last element from an array and returns it.
*/
pop(): T | undefined;
/**
* Combines two or more arrays.
* @param items Additional items to add to the end of array1.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@ tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(25,5): error
Types of property '0' are incompatible.
Type 'string' is not assignable to type 'number'.
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(26,5): error TS2322: Type '{ 0: string; 1: number; length: 2; }' is not assignable to type '[number]'.
Property 'push' is missing in type '{ 0: string; 1: number; length: 2; }'.
Property 'pop' is missing in type '{ 0: string; 1: number; length: 2; }'.
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(27,5): error TS2322: Type '[string, number]' is not assignable to type '[string]'.
Types of property 'length' are incompatible.
Type '2' is not assignable to type '1'.
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(28,5): error TS2322: Type 'StrNum' is not assignable to type '[string]'.
Types of property 'length' are incompatible.
Type '2' is not assignable to type '1'.
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(29,5): error TS2322: Type '{ 0: string; 1: number; length: 2; }' is not assignable to type '[string]'.
Property 'push' is missing in type '{ 0: string; 1: number; length: 2; }'.
Property 'pop' is missing in type '{ 0: string; 1: number; length: 2; }'.
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(30,5): error TS2322: Type '[string, number]' is not assignable to type '[number, string]'.
Type 'string' is not assignable to type 'number'.
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(31,5): error TS2322: Type 'StrNum' is not assignable to type '[number, string]'.
Types of property '0' are incompatible.
Type 'string' is not assignable to type 'number'.
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(32,5): error TS2322: Type '{ 0: string; 1: number; length: 2; }' is not assignable to type '[number, string]'.
Property 'push' is missing in type '{ 0: string; 1: number; length: 2; }'.
Property 'pop' is missing in type '{ 0: string; 1: number; length: 2; }'.


==== tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts (19 errors) ====
Expand Down Expand Up @@ -102,7 +102,7 @@ tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(32,5): error
var l3: [number] = z;
~~
!!! error TS2322: Type '{ 0: string; 1: number; length: 2; }' is not assignable to type '[number]'.
!!! error TS2322: Property 'push' is missing in type '{ 0: string; 1: number; length: 2; }'.
!!! error TS2322: Property 'pop' is missing in type '{ 0: string; 1: number; length: 2; }'.
var m1: [string] = x;
~~
!!! error TS2322: Type '[string, number]' is not assignable to type '[string]'.
Expand All @@ -116,7 +116,7 @@ tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(32,5): error
var m3: [string] = z;
~~
!!! error TS2322: Type '{ 0: string; 1: number; length: 2; }' is not assignable to type '[string]'.
!!! error TS2322: Property 'push' is missing in type '{ 0: string; 1: number; length: 2; }'.
!!! error TS2322: Property 'pop' is missing in type '{ 0: string; 1: number; length: 2; }'.
var n1: [number, string] = x;
~~
!!! error TS2322: Type '[string, number]' is not assignable to type '[number, string]'.
Expand All @@ -129,7 +129,7 @@ tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(32,5): error
var n3: [number, string] = z;
~~
!!! error TS2322: Type '{ 0: string; 1: number; length: 2; }' is not assignable to type '[number, string]'.
!!! error TS2322: Property 'push' is missing in type '{ 0: string; 1: number; length: 2; }'.
!!! error TS2322: Property 'pop' is missing in type '{ 0: string; 1: number; length: 2; }'.
var o1: [string, number] = x;
var o2: [string, number] = y;
var o3: [string, number] = y;
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/arrayAssignmentTest1.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ tests/cases/compiler/arrayAssignmentTest1.ts(77,1): error TS2322: Type 'I1[]' is
Type 'I1' is not assignable to type 'C3'.
Property 'CM3M1' is missing in type 'I1'.
tests/cases/compiler/arrayAssignmentTest1.ts(79,1): error TS2322: Type '() => C1' is not assignable to type 'any[]'.
Property 'push' is missing in type '() => C1'.
Property 'pop' is missing in type '() => C1'.
tests/cases/compiler/arrayAssignmentTest1.ts(80,1): error TS2322: Type '{ one: number; }' is not assignable to type 'any[]'.
Property 'length' is missing in type '{ one: number; }'.
tests/cases/compiler/arrayAssignmentTest1.ts(82,1): error TS2322: Type 'C1' is not assignable to type 'any[]'.
Expand Down Expand Up @@ -177,7 +177,7 @@ tests/cases/compiler/arrayAssignmentTest1.ts(85,1): error TS2322: Type 'I1' is n
arr_any = f1; // should be an error - is
~~~~~~~
!!! error TS2322: Type '() => C1' is not assignable to type 'any[]'.
!!! error TS2322: Property 'push' is missing in type '() => C1'.
!!! error TS2322: Property 'pop' is missing in type '() => C1'.
arr_any = o1; // should be an error - is
~~~~~~~
!!! error TS2322: Type '{ one: number; }' is not assignable to type 'any[]'.
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/arrayAssignmentTest2.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ tests/cases/compiler/arrayAssignmentTest2.ts(49,1): error TS2322: Type 'I1[]' is
Type 'I1' is not assignable to type 'C3'.
Property 'CM3M1' is missing in type 'I1'.
tests/cases/compiler/arrayAssignmentTest2.ts(51,1): error TS2322: Type '() => C1' is not assignable to type 'any[]'.
Property 'push' is missing in type '() => C1'.
Property 'pop' is missing in type '() => C1'.
tests/cases/compiler/arrayAssignmentTest2.ts(52,1): error TS2322: Type '() => any' is not assignable to type 'any[]'.
Property 'push' is missing in type '() => any'.
Property 'pop' is missing in type '() => any'.
tests/cases/compiler/arrayAssignmentTest2.ts(53,1): error TS2322: Type '{ one: number; }' is not assignable to type 'any[]'.
Property 'length' is missing in type '{ one: number; }'.
tests/cases/compiler/arrayAssignmentTest2.ts(55,1): error TS2322: Type 'C1' is not assignable to type 'any[]'.
Expand Down Expand Up @@ -89,11 +89,11 @@ tests/cases/compiler/arrayAssignmentTest2.ts(58,1): error TS2322: Type 'I1' is n
arr_any = f1; // should be an error - is
~~~~~~~
!!! error TS2322: Type '() => C1' is not assignable to type 'any[]'.
!!! error TS2322: Property 'push' is missing in type '() => C1'.
!!! error TS2322: Property 'pop' is missing in type '() => C1'.
arr_any = function () { return null;} // should be an error - is
~~~~~~~
!!! error TS2322: Type '() => any' is not assignable to type 'any[]'.
!!! error TS2322: Property 'push' is missing in type '() => any'.
!!! error TS2322: Property 'pop' is missing in type '() => any'.
arr_any = o1; // should be an error - is
~~~~~~~
!!! error TS2322: Type '{ one: number; }' is not assignable to type 'any[]'.
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/arrayAssignmentTest4.errors.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
tests/cases/compiler/arrayAssignmentTest4.ts(22,1): error TS2322: Type '() => any' is not assignable to type 'any[]'.
Property 'push' is missing in type '() => any'.
Property 'pop' is missing in type '() => any'.
tests/cases/compiler/arrayAssignmentTest4.ts(23,1): error TS2322: Type 'C3' is not assignable to type 'any[]'.
Property 'length' is missing in type 'C3'.

Expand Down Expand Up @@ -29,7 +29,7 @@ tests/cases/compiler/arrayAssignmentTest4.ts(23,1): error TS2322: Type 'C3' is n
arr_any = function () { return null;} // should be an error - is
~~~~~~~
!!! error TS2322: Type '() => any' is not assignable to type 'any[]'.
!!! error TS2322: Property 'push' is missing in type '() => any'.
!!! error TS2322: Property 'pop' is missing in type '() => any'.
arr_any = c3; // should be an error - is
~~~~~~~
!!! error TS2322: Type 'C3' is not assignable to type 'any[]'.
Expand Down
20 changes: 8 additions & 12 deletions tests/baselines/reference/arrayLiterals3.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(32,5): error
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(33,5): error TS2322: Type 'number[]' is not assignable to type '[number, number, number]'.
Property '0' is missing in type 'number[]'.
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(34,5): error TS2322: Type '(string | number)[]' is not assignable to type 'myArray'.
Types of property 'push' are incompatible.
Type '(...items: (string | number)[]) => number' is not assignable to type '(...items: Number[]) => number'.
Types of parameters 'items' and 'items' are incompatible.
Type 'Number' is not assignable to type 'string | number'.
Type 'Number' is not assignable to type 'number'.
'number' is a primitive, but 'Number' is a wrapper object. Prefer using 'number' when possible.
Types of property 'pop' are incompatible.
Type '() => string | number' is not assignable to type '() => Number'.
Type 'string | number' is not assignable to type 'Number'.
Type 'string' is not assignable to type 'Number'.


==== tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts (8 errors) ====
Expand Down Expand Up @@ -75,10 +73,8 @@ tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(34,5): error
var c2: myArray = [...temp1, ...temp]; // Error cannot assign (number|string)[] to number[]
~~
!!! error TS2322: Type '(string | number)[]' is not assignable to type 'myArray'.
!!! error TS2322: Types of property 'push' are incompatible.
!!! error TS2322: Type '(...items: (string | number)[]) => number' is not assignable to type '(...items: Number[]) => number'.
!!! error TS2322: Types of parameters 'items' and 'items' are incompatible.
!!! error TS2322: Type 'Number' is not assignable to type 'string | number'.
!!! error TS2322: Type 'Number' is not assignable to type 'number'.
!!! error TS2322: 'number' is a primitive, but 'Number' is a wrapper object. Prefer using 'number' when possible.
!!! error TS2322: Types of property 'pop' are incompatible.
!!! error TS2322: Type '() => string | number' is not assignable to type '() => Number'.
!!! error TS2322: Type 'string | number' is not assignable to type 'Number'.
!!! error TS2322: Type 'string' is not assignable to type 'Number'.

4 changes: 2 additions & 2 deletions tests/baselines/reference/keyofAndIndexedAccess.types
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ type K10 = keyof Shape; // "name" | "width" | "height" | "visible"
>Shape : Shape

type K11 = keyof Shape[]; // "length" | "toString" | ...
>K11 : number | "length" | "toString" | "toLocaleString" | "push" | "pop" | "concat" | "join" | "reverse" | "shift" | "slice" | "sort" | "splice" | "unshift" | "indexOf" | "lastIndexOf" | "every" | "some" | "forEach" | "map" | "filter" | "reduce" | "reduceRight"
>K11 : number | "length" | "toString" | "toLocaleString" | "pop" | "push" | "concat" | "join" | "reverse" | "shift" | "slice" | "sort" | "splice" | "unshift" | "indexOf" | "lastIndexOf" | "every" | "some" | "forEach" | "map" | "filter" | "reduce" | "reduceRight"
>Shape : Shape

type K12 = keyof Dictionary<Shape>; // string
Expand All @@ -108,7 +108,7 @@ type K15 = keyof E; // "toString" | "toFixed" | "toExponential" | ...
>E : E

type K16 = keyof [string, number]; // "0" | "1" | "length" | "toString" | ...
>K16 : number | "0" | "1" | "length" | "toString" | "toLocaleString" | "push" | "pop" | "concat" | "join" | "reverse" | "shift" | "slice" | "sort" | "splice" | "unshift" | "indexOf" | "lastIndexOf" | "every" | "some" | "forEach" | "map" | "filter" | "reduce" | "reduceRight"
>K16 : number | "0" | "1" | "length" | "toString" | "toLocaleString" | "pop" | "push" | "concat" | "join" | "reverse" | "shift" | "slice" | "sort" | "splice" | "unshift" | "indexOf" | "lastIndexOf" | "every" | "some" | "forEach" | "map" | "filter" | "reduce" | "reduceRight"

type K17 = keyof (Shape | Item); // "name"
>K17 : "name"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
tests/cases/compiler/nestedRecursiveArraysOrObjectsError01.ts(10,9): error TS2322: Type '{ foo: string; jj: number; }[][][]' is not assignable to type 'Style'.
Type '{ foo: string; jj: number; }[][][]' is not assignable to type 'StyleArray'.
Types of property 'pop' are incompatible.
Type '() => { foo: string; jj: number; }[][]' is not assignable to type '() => Style'.
Type '{ foo: string; jj: number; }[][]' is not assignable to type 'Style'.
Type '{ foo: string; jj: number; }[][]' is not assignable to type 'StyleArray'.
Types of property 'pop' are incompatible.
Type '() => { foo: string; jj: number; }[]' is not assignable to type '() => Style'.
Type '{ foo: string; jj: number; }[]' is not assignable to type 'Style'.
Type '{ foo: string; jj: number; }[]' is not assignable to type 'StyleArray'.
Types of property 'pop' are incompatible.
Type '() => { foo: string; jj: number; }' is not assignable to type '() => Style'.
Type '{ foo: string; jj: number; }' is not assignable to type 'Style'.
Object literal may only specify known properties, and 'jj' does not exist in type 'Style'.


==== tests/cases/compiler/nestedRecursiveArraysOrObjectsError01.ts (1 errors) ====
type Style = StyleBase | StyleArray;
interface StyleArray extends Array<Style> {}
interface StyleBase {
foo: string;
}

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


23 changes: 23 additions & 0 deletions tests/baselines/reference/nestedRecursiveArraysOrObjectsError01.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//// [nestedRecursiveArraysOrObjectsError01.ts]
type Style = StyleBase | StyleArray;
interface StyleArray extends Array<Style> {}
interface StyleBase {
foo: string;
}

const blah: Style = [
[[{
foo: 'asdf',
jj: 1 // intentional error
}]]
];



//// [nestedRecursiveArraysOrObjectsError01.js]
var blah = [
[[{
foo: 'asdf',
jj: 1 // intentional error
}]]
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
=== tests/cases/compiler/nestedRecursiveArraysOrObjectsError01.ts ===
type Style = StyleBase | StyleArray;
>Style : Symbol(Style, Decl(nestedRecursiveArraysOrObjectsError01.ts, 0, 0))
>StyleBase : Symbol(StyleBase, Decl(nestedRecursiveArraysOrObjectsError01.ts, 1, 44))
>StyleArray : Symbol(StyleArray, Decl(nestedRecursiveArraysOrObjectsError01.ts, 0, 36))

interface StyleArray extends Array<Style> {}
>StyleArray : Symbol(StyleArray, Decl(nestedRecursiveArraysOrObjectsError01.ts, 0, 36))
>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>Style : Symbol(Style, Decl(nestedRecursiveArraysOrObjectsError01.ts, 0, 0))

interface StyleBase {
>StyleBase : Symbol(StyleBase, Decl(nestedRecursiveArraysOrObjectsError01.ts, 1, 44))

foo: string;
>foo : Symbol(StyleBase.foo, Decl(nestedRecursiveArraysOrObjectsError01.ts, 2, 21))
}

const blah: Style = [
>blah : Symbol(blah, Decl(nestedRecursiveArraysOrObjectsError01.ts, 6, 5))
>Style : Symbol(Style, Decl(nestedRecursiveArraysOrObjectsError01.ts, 0, 0))

[[{
foo: 'asdf',
>foo : Symbol(foo, Decl(nestedRecursiveArraysOrObjectsError01.ts, 7, 7))

jj: 1 // intentional error
>jj : Symbol(jj, Decl(nestedRecursiveArraysOrObjectsError01.ts, 8, 20))

}]]
];


Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
=== tests/cases/compiler/nestedRecursiveArraysOrObjectsError01.ts ===
type Style = StyleBase | StyleArray;
>Style : Style
>StyleBase : StyleBase
>StyleArray : StyleArray

interface StyleArray extends Array<Style> {}
>StyleArray : StyleArray
>Array : T[]
>Style : Style

interface StyleBase {
>StyleBase : StyleBase

foo: string;
>foo : string
}

const blah: Style = [
>blah : Style
>Style : Style
>[ [[{ foo: 'asdf', jj: 1 // intentional error }]]] : { foo: string; jj: number; }[][][]

[[{
>[[{ foo: 'asdf', jj: 1 // intentional error }]] : { foo: string; jj: number; }[][]
>[{ foo: 'asdf', jj: 1 // intentional error }] : { foo: string; jj: number; }[]
>{ foo: 'asdf', jj: 1 // intentional error } : { foo: string; jj: number; }

foo: 'asdf',
>foo : string
>'asdf' : "asdf"

jj: 1 // intentional error
>jj : number
>1 : 1

}]]
];


Loading

0 comments on commit 93ab352

Please sign in to comment.