From 9a57ff70d851bb6a470251d2c1bc1feb91ab869b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Fri, 25 Mar 2022 10:30:02 +0100 Subject: [PATCH] Add explicit tests for unconstrained type param not being assignable to `{}` and `Record` --- ...UnboundedTypeParamAssignability.errors.txt | 36 +++++++++++ .../genericUnboundedTypeParamAssignability.js | 38 ++++++++++++ ...ricUnboundedTypeParamAssignability.symbols | 58 ++++++++++++++++++ ...nericUnboundedTypeParamAssignability.types | 60 +++++++++++++++++++ .../genericUnboundedTypeParamAssignability.ts | 20 +++++++ 5 files changed, 212 insertions(+) create mode 100644 tests/baselines/reference/genericUnboundedTypeParamAssignability.errors.txt create mode 100644 tests/baselines/reference/genericUnboundedTypeParamAssignability.js create mode 100644 tests/baselines/reference/genericUnboundedTypeParamAssignability.symbols create mode 100644 tests/baselines/reference/genericUnboundedTypeParamAssignability.types create mode 100644 tests/cases/compiler/genericUnboundedTypeParamAssignability.ts diff --git a/tests/baselines/reference/genericUnboundedTypeParamAssignability.errors.txt b/tests/baselines/reference/genericUnboundedTypeParamAssignability.errors.txt new file mode 100644 index 0000000000000..6d7b87ebd3351 --- /dev/null +++ b/tests/baselines/reference/genericUnboundedTypeParamAssignability.errors.txt @@ -0,0 +1,36 @@ +tests/cases/compiler/genericUnboundedTypeParamAssignability.ts(2,5): error TS2339: Property 'toString' does not exist on type 'T'. +tests/cases/compiler/genericUnboundedTypeParamAssignability.ts(15,6): error TS2345: Argument of type 'T' is not assignable to parameter of type '{}'. +tests/cases/compiler/genericUnboundedTypeParamAssignability.ts(16,6): error TS2345: Argument of type 'T' is not assignable to parameter of type 'Record'. +tests/cases/compiler/genericUnboundedTypeParamAssignability.ts(17,5): error TS2339: Property 'toString' does not exist on type 'T'. + + +==== tests/cases/compiler/genericUnboundedTypeParamAssignability.ts (4 errors) ==== + function f1(o: T) { + o.toString(); // error + ~~~~~~~~ +!!! error TS2339: Property 'toString' does not exist on type 'T'. + } + + function f2(o: T) { + o.toString(); // no error + } + + function f3>(o: T) { + o.toString(); // no error + } + + function user(t: T) { + f1(t); + f2(t); // error in strict, unbounded T doesn't satisfy the constraint + ~ +!!! error TS2345: Argument of type 'T' is not assignable to parameter of type '{}'. +!!! related TS2208 tests/cases/compiler/genericUnboundedTypeParamAssignability.ts:13:15: This type parameter probably needs an `extends object` constraint. + f3(t); // error in strict, unbounded T doesn't satisfy the constraint + ~ +!!! error TS2345: Argument of type 'T' is not assignable to parameter of type 'Record'. +!!! related TS2208 tests/cases/compiler/genericUnboundedTypeParamAssignability.ts:13:15: This type parameter probably needs an `extends object` constraint. + t.toString(); // error, for the same reason as f1() + ~~~~~~~~ +!!! error TS2339: Property 'toString' does not exist on type 'T'. + } + \ No newline at end of file diff --git a/tests/baselines/reference/genericUnboundedTypeParamAssignability.js b/tests/baselines/reference/genericUnboundedTypeParamAssignability.js new file mode 100644 index 0000000000000..9c13758f22b6c --- /dev/null +++ b/tests/baselines/reference/genericUnboundedTypeParamAssignability.js @@ -0,0 +1,38 @@ +//// [genericUnboundedTypeParamAssignability.ts] +function f1(o: T) { + o.toString(); // error +} + +function f2(o: T) { + o.toString(); // no error +} + +function f3>(o: T) { + o.toString(); // no error +} + +function user(t: T) { + f1(t); + f2(t); // error in strict, unbounded T doesn't satisfy the constraint + f3(t); // error in strict, unbounded T doesn't satisfy the constraint + t.toString(); // error, for the same reason as f1() +} + + +//// [genericUnboundedTypeParamAssignability.js] +"use strict"; +function f1(o) { + o.toString(); // error +} +function f2(o) { + o.toString(); // no error +} +function f3(o) { + o.toString(); // no error +} +function user(t) { + f1(t); + f2(t); // error in strict, unbounded T doesn't satisfy the constraint + f3(t); // error in strict, unbounded T doesn't satisfy the constraint + t.toString(); // error, for the same reason as f1() +} diff --git a/tests/baselines/reference/genericUnboundedTypeParamAssignability.symbols b/tests/baselines/reference/genericUnboundedTypeParamAssignability.symbols new file mode 100644 index 0000000000000..b394adbbacb09 --- /dev/null +++ b/tests/baselines/reference/genericUnboundedTypeParamAssignability.symbols @@ -0,0 +1,58 @@ +=== tests/cases/compiler/genericUnboundedTypeParamAssignability.ts === +function f1(o: T) { +>f1 : Symbol(f1, Decl(genericUnboundedTypeParamAssignability.ts, 0, 0)) +>T : Symbol(T, Decl(genericUnboundedTypeParamAssignability.ts, 0, 12)) +>o : Symbol(o, Decl(genericUnboundedTypeParamAssignability.ts, 0, 15)) +>T : Symbol(T, Decl(genericUnboundedTypeParamAssignability.ts, 0, 12)) + + o.toString(); // error +>o : Symbol(o, Decl(genericUnboundedTypeParamAssignability.ts, 0, 15)) +} + +function f2(o: T) { +>f2 : Symbol(f2, Decl(genericUnboundedTypeParamAssignability.ts, 2, 1)) +>T : Symbol(T, Decl(genericUnboundedTypeParamAssignability.ts, 4, 12)) +>o : Symbol(o, Decl(genericUnboundedTypeParamAssignability.ts, 4, 26)) +>T : Symbol(T, Decl(genericUnboundedTypeParamAssignability.ts, 4, 12)) + + o.toString(); // no error +>o.toString : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --)) +>o : Symbol(o, Decl(genericUnboundedTypeParamAssignability.ts, 4, 26)) +>toString : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --)) +} + +function f3>(o: T) { +>f3 : Symbol(f3, Decl(genericUnboundedTypeParamAssignability.ts, 6, 1)) +>T : Symbol(T, Decl(genericUnboundedTypeParamAssignability.ts, 8, 12)) +>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) +>o : Symbol(o, Decl(genericUnboundedTypeParamAssignability.ts, 8, 43)) +>T : Symbol(T, Decl(genericUnboundedTypeParamAssignability.ts, 8, 12)) + + o.toString(); // no error +>o.toString : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --)) +>o : Symbol(o, Decl(genericUnboundedTypeParamAssignability.ts, 8, 43)) +>toString : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --)) +} + +function user(t: T) { +>user : Symbol(user, Decl(genericUnboundedTypeParamAssignability.ts, 10, 1)) +>T : Symbol(T, Decl(genericUnboundedTypeParamAssignability.ts, 12, 14)) +>t : Symbol(t, Decl(genericUnboundedTypeParamAssignability.ts, 12, 17)) +>T : Symbol(T, Decl(genericUnboundedTypeParamAssignability.ts, 12, 14)) + + f1(t); +>f1 : Symbol(f1, Decl(genericUnboundedTypeParamAssignability.ts, 0, 0)) +>t : Symbol(t, Decl(genericUnboundedTypeParamAssignability.ts, 12, 17)) + + f2(t); // error in strict, unbounded T doesn't satisfy the constraint +>f2 : Symbol(f2, Decl(genericUnboundedTypeParamAssignability.ts, 2, 1)) +>t : Symbol(t, Decl(genericUnboundedTypeParamAssignability.ts, 12, 17)) + + f3(t); // error in strict, unbounded T doesn't satisfy the constraint +>f3 : Symbol(f3, Decl(genericUnboundedTypeParamAssignability.ts, 6, 1)) +>t : Symbol(t, Decl(genericUnboundedTypeParamAssignability.ts, 12, 17)) + + t.toString(); // error, for the same reason as f1() +>t : Symbol(t, Decl(genericUnboundedTypeParamAssignability.ts, 12, 17)) +} + diff --git a/tests/baselines/reference/genericUnboundedTypeParamAssignability.types b/tests/baselines/reference/genericUnboundedTypeParamAssignability.types new file mode 100644 index 0000000000000..74136a19382a6 --- /dev/null +++ b/tests/baselines/reference/genericUnboundedTypeParamAssignability.types @@ -0,0 +1,60 @@ +=== tests/cases/compiler/genericUnboundedTypeParamAssignability.ts === +function f1(o: T) { +>f1 : (o: T) => void +>o : T + + o.toString(); // error +>o.toString() : any +>o.toString : any +>o : T +>toString : any +} + +function f2(o: T) { +>f2 : (o: T) => void +>o : T + + o.toString(); // no error +>o.toString() : string +>o.toString : () => string +>o : T +>toString : () => string +} + +function f3>(o: T) { +>f3 : >(o: T) => void +>o : T + + o.toString(); // no error +>o.toString() : string +>o.toString : () => string +>o : T +>toString : () => string +} + +function user(t: T) { +>user : (t: T) => void +>t : T + + f1(t); +>f1(t) : void +>f1 : (o: T) => void +>t : T + + f2(t); // error in strict, unbounded T doesn't satisfy the constraint +>f2(t) : void +>f2 : (o: T) => void +>t : T + + f3(t); // error in strict, unbounded T doesn't satisfy the constraint +>f3(t) : void +>f3 : >(o: T) => void +>t : T + + t.toString(); // error, for the same reason as f1() +>t.toString() : any +>t.toString : any +>t : T +>toString : any +} + diff --git a/tests/cases/compiler/genericUnboundedTypeParamAssignability.ts b/tests/cases/compiler/genericUnboundedTypeParamAssignability.ts new file mode 100644 index 0000000000000..542e96753ff2d --- /dev/null +++ b/tests/cases/compiler/genericUnboundedTypeParamAssignability.ts @@ -0,0 +1,20 @@ +// @strict: true + +function f1(o: T) { + o.toString(); // error +} + +function f2(o: T) { + o.toString(); // no error +} + +function f3>(o: T) { + o.toString(); // no error +} + +function user(t: T) { + f1(t); + f2(t); // error in strict, unbounded T doesn't satisfy the constraint + f3(t); // error in strict, unbounded T doesn't satisfy the constraint + t.toString(); // error, for the same reason as f1() +}