From aae7572c4869dd5a62acd4311e8f7fdefd1f05f6 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 5 Oct 2017 13:25:23 -0700 Subject: [PATCH] Add test --- .../strictFunctionTypesErrors.errors.txt | 29 ++++++++++++++++++- .../reference/strictFunctionTypesErrors.js | 9 ++++++ .../strictFunctionTypesErrors.symbols | 24 +++++++++++++++ .../reference/strictFunctionTypesErrors.types | 26 +++++++++++++++++ .../compiler/strictFunctionTypesErrors.ts | 7 +++++ 5 files changed, 94 insertions(+), 1 deletion(-) diff --git a/tests/baselines/reference/strictFunctionTypesErrors.errors.txt b/tests/baselines/reference/strictFunctionTypesErrors.errors.txt index 661ad437ed2..6a8204fb147 100644 --- a/tests/baselines/reference/strictFunctionTypesErrors.errors.txt +++ b/tests/baselines/reference/strictFunctionTypesErrors.errors.txt @@ -83,9 +83,18 @@ tests/cases/compiler/strictFunctionTypesErrors.ts(126,1): error TS2322: Type 'Cr tests/cases/compiler/strictFunctionTypesErrors.ts(127,1): error TS2322: Type 'Crate' is not assignable to type 'Crate'. Types of property 'item' are incompatible. Type 'Animal' is not assignable to type 'Dog'. +tests/cases/compiler/strictFunctionTypesErrors.ts(133,1): error TS2322: Type '(f: (x: Dog) => Dog) => void' is not assignable to type '(f: (x: Animal) => Animal) => void'. + Types of parameters 'f' and 'f' are incompatible. + Type '(x: Animal) => Animal' is not assignable to type '(x: Dog) => Dog'. + Type 'Animal' is not assignable to type 'Dog'. +tests/cases/compiler/strictFunctionTypesErrors.ts(134,1): error TS2322: Type '(f: (x: Animal) => Animal) => void' is not assignable to type '(f: (x: Dog) => Dog) => void'. + Types of parameters 'f' and 'f' are incompatible. + Type '(x: Dog) => Dog' is not assignable to type '(x: Animal) => Animal'. + Types of parameters 'x' and 'x' are incompatible. + Type 'Animal' is not assignable to type 'Dog'. -==== tests/cases/compiler/strictFunctionTypesErrors.ts (31 errors) ==== +==== tests/cases/compiler/strictFunctionTypesErrors.ts (33 errors) ==== export {} @@ -329,4 +338,22 @@ tests/cases/compiler/strictFunctionTypesErrors.ts(127,1): error TS2322: Type 'Cr !!! error TS2322: Type 'Crate' is not assignable to type 'Crate'. !!! error TS2322: Types of property 'item' are incompatible. !!! error TS2322: Type 'Animal' is not assignable to type 'Dog'. + + // Verify that callback parameters are strictly checked + + declare let fc1: (f: (x: Animal) => Animal) => void; + declare let fc2: (f: (x: Dog) => Dog) => void; + fc1 = fc2; // Error + ~~~ +!!! error TS2322: Type '(f: (x: Dog) => Dog) => void' is not assignable to type '(f: (x: Animal) => Animal) => void'. +!!! error TS2322: Types of parameters 'f' and 'f' are incompatible. +!!! error TS2322: Type '(x: Animal) => Animal' is not assignable to type '(x: Dog) => Dog'. +!!! error TS2322: Type 'Animal' is not assignable to type 'Dog'. + fc2 = fc1; // Error + ~~~ +!!! error TS2322: Type '(f: (x: Animal) => Animal) => void' is not assignable to type '(f: (x: Dog) => Dog) => void'. +!!! error TS2322: Types of parameters 'f' and 'f' are incompatible. +!!! error TS2322: Type '(x: Dog) => Dog' is not assignable to type '(x: Animal) => Animal'. +!!! error TS2322: Types of parameters 'x' and 'x' are incompatible. +!!! error TS2322: Type 'Animal' is not assignable to type 'Dog'. \ No newline at end of file diff --git a/tests/baselines/reference/strictFunctionTypesErrors.js b/tests/baselines/reference/strictFunctionTypesErrors.js index 2be598f0ef9..8e338fa030d 100644 --- a/tests/baselines/reference/strictFunctionTypesErrors.js +++ b/tests/baselines/reference/strictFunctionTypesErrors.js @@ -126,6 +126,13 @@ declare let dogCrate: Crate; animalCrate = dogCrate; // Error dogCrate = animalCrate; // Error + +// Verify that callback parameters are strictly checked + +declare let fc1: (f: (x: Animal) => Animal) => void; +declare let fc2: (f: (x: Dog) => Dog) => void; +fc1 = fc2; // Error +fc2 = fc1; // Error //// [strictFunctionTypesErrors.js] @@ -186,3 +193,5 @@ dogComparer2 = animalComparer2; // Ok // Errors below should elaborate the reason for invariance animalCrate = dogCrate; // Error dogCrate = animalCrate; // Error +fc1 = fc2; // Error +fc2 = fc1; // Error diff --git a/tests/baselines/reference/strictFunctionTypesErrors.symbols b/tests/baselines/reference/strictFunctionTypesErrors.symbols index 30faf83d87e..fd28775c5e1 100644 --- a/tests/baselines/reference/strictFunctionTypesErrors.symbols +++ b/tests/baselines/reference/strictFunctionTypesErrors.symbols @@ -400,3 +400,27 @@ dogCrate = animalCrate; // Error >dogCrate : Symbol(dogCrate, Decl(strictFunctionTypesErrors.ts, 121, 11)) >animalCrate : Symbol(animalCrate, Decl(strictFunctionTypesErrors.ts, 120, 11)) +// Verify that callback parameters are strictly checked + +declare let fc1: (f: (x: Animal) => Animal) => void; +>fc1 : Symbol(fc1, Decl(strictFunctionTypesErrors.ts, 130, 11)) +>f : Symbol(f, Decl(strictFunctionTypesErrors.ts, 130, 18)) +>x : Symbol(x, Decl(strictFunctionTypesErrors.ts, 130, 22)) +>Animal : Symbol(Animal, Decl(strictFunctionTypesErrors.ts, 87, 8)) +>Animal : Symbol(Animal, Decl(strictFunctionTypesErrors.ts, 87, 8)) + +declare let fc2: (f: (x: Dog) => Dog) => void; +>fc2 : Symbol(fc2, Decl(strictFunctionTypesErrors.ts, 131, 11)) +>f : Symbol(f, Decl(strictFunctionTypesErrors.ts, 131, 18)) +>x : Symbol(x, Decl(strictFunctionTypesErrors.ts, 131, 22)) +>Dog : Symbol(Dog, Decl(strictFunctionTypesErrors.ts, 89, 33)) +>Dog : Symbol(Dog, Decl(strictFunctionTypesErrors.ts, 89, 33)) + +fc1 = fc2; // Error +>fc1 : Symbol(fc1, Decl(strictFunctionTypesErrors.ts, 130, 11)) +>fc2 : Symbol(fc2, Decl(strictFunctionTypesErrors.ts, 131, 11)) + +fc2 = fc1; // Error +>fc2 : Symbol(fc2, Decl(strictFunctionTypesErrors.ts, 131, 11)) +>fc1 : Symbol(fc1, Decl(strictFunctionTypesErrors.ts, 130, 11)) + diff --git a/tests/baselines/reference/strictFunctionTypesErrors.types b/tests/baselines/reference/strictFunctionTypesErrors.types index e4372d4b8fa..45deeebabab 100644 --- a/tests/baselines/reference/strictFunctionTypesErrors.types +++ b/tests/baselines/reference/strictFunctionTypesErrors.types @@ -454,3 +454,29 @@ dogCrate = animalCrate; // Error >dogCrate : Crate >animalCrate : Crate +// Verify that callback parameters are strictly checked + +declare let fc1: (f: (x: Animal) => Animal) => void; +>fc1 : (f: (x: Animal) => Animal) => void +>f : (x: Animal) => Animal +>x : Animal +>Animal : Animal +>Animal : Animal + +declare let fc2: (f: (x: Dog) => Dog) => void; +>fc2 : (f: (x: Dog) => Dog) => void +>f : (x: Dog) => Dog +>x : Dog +>Dog : Dog +>Dog : Dog + +fc1 = fc2; // Error +>fc1 = fc2 : (f: (x: Dog) => Dog) => void +>fc1 : (f: (x: Animal) => Animal) => void +>fc2 : (f: (x: Dog) => Dog) => void + +fc2 = fc1; // Error +>fc2 = fc1 : (f: (x: Animal) => Animal) => void +>fc2 : (f: (x: Dog) => Dog) => void +>fc1 : (f: (x: Animal) => Animal) => void + diff --git a/tests/cases/compiler/strictFunctionTypesErrors.ts b/tests/cases/compiler/strictFunctionTypesErrors.ts index fbf1c0fa6da..95598d9d444 100644 --- a/tests/cases/compiler/strictFunctionTypesErrors.ts +++ b/tests/cases/compiler/strictFunctionTypesErrors.ts @@ -126,3 +126,10 @@ declare let dogCrate: Crate; animalCrate = dogCrate; // Error dogCrate = animalCrate; // Error + +// Verify that callback parameters are strictly checked + +declare let fc1: (f: (x: Animal) => Animal) => void; +declare let fc2: (f: (x: Dog) => Dog) => void; +fc1 = fc2; // Error +fc2 = fc1; // Error