-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Fix type parameter comparability to consistently allow comparisons on unconstrained type parameters #48861
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
Fix type parameter comparability to consistently allow comparisons on unconstrained type parameters #48861
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
tests/cases/compiler/genericWithNoConstraintComparableWithCurlyCurly.ts(23,5): error TS2352: Conversion of type '{}' to type 'T' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first. | ||
'T' could be instantiated with an arbitrary type which could be unrelated to '{}'. | ||
|
||
|
||
==== tests/cases/compiler/genericWithNoConstraintComparableWithCurlyCurly.ts (1 errors) ==== | ||
function foo<T>() { | ||
let x = {}; | ||
x as T; | ||
} | ||
|
||
function bar<T extends unknown>() { | ||
let x = {}; | ||
x as T; | ||
} | ||
|
||
function baz<T extends {}>() { | ||
let x = {}; | ||
x as T; | ||
} | ||
|
||
function bat<T extends object>() { | ||
let x = {}; | ||
x as T; | ||
} | ||
|
||
function no<T extends null | undefined>() { | ||
let x = {}; | ||
x as T; // should error | ||
~~~~~~ | ||
!!! error TS2352: Conversion of type '{}' to type 'T' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first. | ||
!!! error TS2352: 'T' could be instantiated with an arbitrary type which could be unrelated to '{}'. | ||
} | ||
|
||
function yes<T extends object | null | undefined>() { | ||
let x = {}; | ||
x as T; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
//// [genericWithNoConstraintComparableWithCurlyCurly.ts] | ||
function foo<T>() { | ||
let x = {}; | ||
x as T; | ||
} | ||
|
||
function bar<T extends unknown>() { | ||
let x = {}; | ||
x as T; | ||
} | ||
|
||
function baz<T extends {}>() { | ||
let x = {}; | ||
x as T; | ||
} | ||
|
||
function bat<T extends object>() { | ||
let x = {}; | ||
x as T; | ||
} | ||
|
||
function no<T extends null | undefined>() { | ||
let x = {}; | ||
x as T; // should error | ||
} | ||
|
||
function yes<T extends object | null | undefined>() { | ||
let x = {}; | ||
x as T; | ||
} | ||
|
||
//// [genericWithNoConstraintComparableWithCurlyCurly.js] | ||
"use strict"; | ||
function foo() { | ||
var x = {}; | ||
x; | ||
} | ||
function bar() { | ||
var x = {}; | ||
x; | ||
} | ||
function baz() { | ||
var x = {}; | ||
x; | ||
} | ||
function bat() { | ||
var x = {}; | ||
x; | ||
} | ||
function no() { | ||
var x = {}; | ||
x; // should error | ||
} | ||
function yes() { | ||
var x = {}; | ||
x; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
=== tests/cases/compiler/genericWithNoConstraintComparableWithCurlyCurly.ts === | ||
function foo<T>() { | ||
>foo : Symbol(foo, Decl(genericWithNoConstraintComparableWithCurlyCurly.ts, 0, 0)) | ||
>T : Symbol(T, Decl(genericWithNoConstraintComparableWithCurlyCurly.ts, 0, 13)) | ||
|
||
let x = {}; | ||
>x : Symbol(x, Decl(genericWithNoConstraintComparableWithCurlyCurly.ts, 1, 7)) | ||
|
||
x as T; | ||
>x : Symbol(x, Decl(genericWithNoConstraintComparableWithCurlyCurly.ts, 1, 7)) | ||
>T : Symbol(T, Decl(genericWithNoConstraintComparableWithCurlyCurly.ts, 0, 13)) | ||
} | ||
|
||
function bar<T extends unknown>() { | ||
>bar : Symbol(bar, Decl(genericWithNoConstraintComparableWithCurlyCurly.ts, 3, 1)) | ||
>T : Symbol(T, Decl(genericWithNoConstraintComparableWithCurlyCurly.ts, 5, 13)) | ||
|
||
let x = {}; | ||
>x : Symbol(x, Decl(genericWithNoConstraintComparableWithCurlyCurly.ts, 6, 7)) | ||
|
||
x as T; | ||
>x : Symbol(x, Decl(genericWithNoConstraintComparableWithCurlyCurly.ts, 6, 7)) | ||
>T : Symbol(T, Decl(genericWithNoConstraintComparableWithCurlyCurly.ts, 5, 13)) | ||
} | ||
|
||
function baz<T extends {}>() { | ||
>baz : Symbol(baz, Decl(genericWithNoConstraintComparableWithCurlyCurly.ts, 8, 1)) | ||
>T : Symbol(T, Decl(genericWithNoConstraintComparableWithCurlyCurly.ts, 10, 13)) | ||
|
||
let x = {}; | ||
>x : Symbol(x, Decl(genericWithNoConstraintComparableWithCurlyCurly.ts, 11, 7)) | ||
|
||
x as T; | ||
>x : Symbol(x, Decl(genericWithNoConstraintComparableWithCurlyCurly.ts, 11, 7)) | ||
>T : Symbol(T, Decl(genericWithNoConstraintComparableWithCurlyCurly.ts, 10, 13)) | ||
} | ||
|
||
function bat<T extends object>() { | ||
>bat : Symbol(bat, Decl(genericWithNoConstraintComparableWithCurlyCurly.ts, 13, 1)) | ||
>T : Symbol(T, Decl(genericWithNoConstraintComparableWithCurlyCurly.ts, 15, 13)) | ||
|
||
let x = {}; | ||
>x : Symbol(x, Decl(genericWithNoConstraintComparableWithCurlyCurly.ts, 16, 7)) | ||
|
||
x as T; | ||
>x : Symbol(x, Decl(genericWithNoConstraintComparableWithCurlyCurly.ts, 16, 7)) | ||
>T : Symbol(T, Decl(genericWithNoConstraintComparableWithCurlyCurly.ts, 15, 13)) | ||
} | ||
|
||
function no<T extends null | undefined>() { | ||
>no : Symbol(no, Decl(genericWithNoConstraintComparableWithCurlyCurly.ts, 18, 1)) | ||
>T : Symbol(T, Decl(genericWithNoConstraintComparableWithCurlyCurly.ts, 20, 12)) | ||
|
||
let x = {}; | ||
>x : Symbol(x, Decl(genericWithNoConstraintComparableWithCurlyCurly.ts, 21, 7)) | ||
|
||
x as T; // should error | ||
>x : Symbol(x, Decl(genericWithNoConstraintComparableWithCurlyCurly.ts, 21, 7)) | ||
>T : Symbol(T, Decl(genericWithNoConstraintComparableWithCurlyCurly.ts, 20, 12)) | ||
} | ||
|
||
function yes<T extends object | null | undefined>() { | ||
>yes : Symbol(yes, Decl(genericWithNoConstraintComparableWithCurlyCurly.ts, 23, 1)) | ||
>T : Symbol(T, Decl(genericWithNoConstraintComparableWithCurlyCurly.ts, 25, 13)) | ||
|
||
let x = {}; | ||
>x : Symbol(x, Decl(genericWithNoConstraintComparableWithCurlyCurly.ts, 26, 7)) | ||
|
||
x as T; | ||
>x : Symbol(x, Decl(genericWithNoConstraintComparableWithCurlyCurly.ts, 26, 7)) | ||
>T : Symbol(T, Decl(genericWithNoConstraintComparableWithCurlyCurly.ts, 25, 13)) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
=== tests/cases/compiler/genericWithNoConstraintComparableWithCurlyCurly.ts === | ||
function foo<T>() { | ||
>foo : <T>() => void | ||
|
||
let x = {}; | ||
>x : {} | ||
>{} : {} | ||
|
||
x as T; | ||
>x as T : T | ||
>x : {} | ||
} | ||
|
||
function bar<T extends unknown>() { | ||
>bar : <T extends unknown>() => void | ||
|
||
let x = {}; | ||
>x : {} | ||
>{} : {} | ||
|
||
x as T; | ||
>x as T : T | ||
>x : {} | ||
} | ||
|
||
function baz<T extends {}>() { | ||
>baz : <T extends {}>() => void | ||
|
||
let x = {}; | ||
>x : {} | ||
>{} : {} | ||
|
||
x as T; | ||
>x as T : T | ||
>x : {} | ||
} | ||
|
||
function bat<T extends object>() { | ||
>bat : <T extends object>() => void | ||
|
||
let x = {}; | ||
>x : {} | ||
>{} : {} | ||
|
||
x as T; | ||
>x as T : T | ||
>x : {} | ||
} | ||
|
||
function no<T extends null | undefined>() { | ||
>no : <T extends null | undefined>() => void | ||
>null : null | ||
|
||
let x = {}; | ||
>x : {} | ||
>{} : {} | ||
|
||
x as T; // should error | ||
>x as T : T | ||
>x : {} | ||
} | ||
|
||
function yes<T extends object | null | undefined>() { | ||
>yes : <T extends object | null | undefined>() => void | ||
>null : null | ||
|
||
let x = {}; | ||
>x : {} | ||
>{} : {} | ||
|
||
x as T; | ||
>x as T : T | ||
>x : {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,11 +59,11 @@ function f4<T>(x: T & 1 | T & 2) { | |
|
||
case 1: x; break; // T & 1 | ||
>1 : 1 | ||
>x : T & 1 | ||
>x : (T & 1) | (T & 2) | ||
|
||
case 2: x; break; // T & 2 | ||
>2 : 2 | ||
>x : T & 2 | ||
>x : (T & 1) | (T & 2) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fallout from permissive comparability: We use comparability in switch-case narrowing (in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (It's likely a similar change occurs in narrowing assignments, since we also use bidirectional comparability there, and maybe in other places more subtly since we use unidirectional comparability in |
||
|
||
default: x; // Should narrow to never | ||
>x : never | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// @strict: true | ||
function foo<T>() { | ||
let x = {}; | ||
x as T; | ||
} | ||
|
||
function bar<T extends unknown>() { | ||
let x = {}; | ||
x as T; | ||
} | ||
|
||
function baz<T extends {}>() { | ||
let x = {}; | ||
x as T; | ||
} | ||
|
||
function bat<T extends object>() { | ||
let x = {}; | ||
x as T; | ||
} | ||
|
||
function no<T extends null | undefined>() { | ||
let x = {}; | ||
x as T; // should error | ||
} | ||
|
||
function yes<T extends object | null | undefined>() { | ||
let x = {}; | ||
x as T; | ||
} |
Uh oh!
There was an error while loading. Please reload this page.