Skip to content

Unconstrained type variable not assignable to 'object' #23806

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

Merged
merged 5 commits into from
May 1, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10716,19 +10716,18 @@ namespace ts {
return result;
}
}
let constraint = getConstraintForRelation(<TypeParameter>source);
// A type variable with no constraint is not related to the non-primitive object type.
if (constraint || !(target.flags & TypeFlags.NonPrimitive)) {
if (!constraint || constraint.flags & TypeFlags.Any) {
constraint = emptyObjectType;
}
// Report constraint errors only if the constraint is not the empty object type
const reportConstraintErrors = reportErrors && constraint !== emptyObjectType;
if (result = isRelatedTo(constraint, target, reportConstraintErrors)) {
const constraint = getConstraintForRelation(<TypeParameter>source);
if (!constraint || constraint.flags & TypeFlags.Any) {
// A type variable with no constraint is not related to the non-primitive object type.
if (result = isRelatedTo(emptyObjectType, extractTypesOfKind(target, ~TypeFlags.NonPrimitive))) {
errorInfo = saveErrorInfo;
return result;
}
}
else if (result = isRelatedTo(constraint, target, reportErrors)) {
errorInfo = saveErrorInfo;
return result;
}
}
else if (source.flags & TypeFlags.Index) {
if (result = isRelatedTo(keyofConstraintType, target, reportErrors)) {
Expand Down
24 changes: 24 additions & 0 deletions tests/baselines/reference/nonPrimitiveAndTypeVariables.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
tests/cases/conformance/types/nonPrimitive/nonPrimitiveAndTypeVariables.ts(10,9): error TS2322: Type 'T' is not assignable to type 'object'.
tests/cases/conformance/types/nonPrimitive/nonPrimitiveAndTypeVariables.ts(11,9): error TS2322: Type 'T' is not assignable to type 'object | U'.
Type 'T' is not assignable to type 'U'.


==== tests/cases/conformance/types/nonPrimitive/nonPrimitiveAndTypeVariables.ts (2 errors) ====
// Repros from #23800

type A<T, V> = { [P in keyof T]: T[P] extends V ? 1 : 0; };
type B<T, V> = { [P in keyof T]: T[P] extends V | object ? 1 : 0; };

type a = A<{ a: 0 | 1 }, 0>; // { a: 0; }
type b = B<{ a: 0 | 1 }, 0>; // { a: 0; }

function foo<T, U>(x: T) {
let a: object = x; // Error
~
!!! error TS2322: Type 'T' is not assignable to type 'object'.
let b: U | object = x; // Error
~
!!! error TS2322: Type 'T' is not assignable to type 'object | U'.
!!! error TS2322: Type 'T' is not assignable to type 'U'.
}

22 changes: 22 additions & 0 deletions tests/baselines/reference/nonPrimitiveAndTypeVariables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//// [nonPrimitiveAndTypeVariables.ts]
// Repros from #23800

type A<T, V> = { [P in keyof T]: T[P] extends V ? 1 : 0; };
type B<T, V> = { [P in keyof T]: T[P] extends V | object ? 1 : 0; };

type a = A<{ a: 0 | 1 }, 0>; // { a: 0; }
type b = B<{ a: 0 | 1 }, 0>; // { a: 0; }

function foo<T, U>(x: T) {
let a: object = x; // Error
let b: U | object = x; // Error
}


//// [nonPrimitiveAndTypeVariables.js]
"use strict";
// Repros from #23800
function foo(x) {
var a = x; // Error
var b = x; // Error
}
50 changes: 50 additions & 0 deletions tests/baselines/reference/nonPrimitiveAndTypeVariables.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
=== tests/cases/conformance/types/nonPrimitive/nonPrimitiveAndTypeVariables.ts ===
// Repros from #23800

type A<T, V> = { [P in keyof T]: T[P] extends V ? 1 : 0; };
>A : Symbol(A, Decl(nonPrimitiveAndTypeVariables.ts, 0, 0))
>T : Symbol(T, Decl(nonPrimitiveAndTypeVariables.ts, 2, 7))
>V : Symbol(V, Decl(nonPrimitiveAndTypeVariables.ts, 2, 9))
>P : Symbol(P, Decl(nonPrimitiveAndTypeVariables.ts, 2, 18))
>T : Symbol(T, Decl(nonPrimitiveAndTypeVariables.ts, 2, 7))
>T : Symbol(T, Decl(nonPrimitiveAndTypeVariables.ts, 2, 7))
>P : Symbol(P, Decl(nonPrimitiveAndTypeVariables.ts, 2, 18))
>V : Symbol(V, Decl(nonPrimitiveAndTypeVariables.ts, 2, 9))

type B<T, V> = { [P in keyof T]: T[P] extends V | object ? 1 : 0; };
>B : Symbol(B, Decl(nonPrimitiveAndTypeVariables.ts, 2, 59))
>T : Symbol(T, Decl(nonPrimitiveAndTypeVariables.ts, 3, 7))
>V : Symbol(V, Decl(nonPrimitiveAndTypeVariables.ts, 3, 9))
>P : Symbol(P, Decl(nonPrimitiveAndTypeVariables.ts, 3, 18))
>T : Symbol(T, Decl(nonPrimitiveAndTypeVariables.ts, 3, 7))
>T : Symbol(T, Decl(nonPrimitiveAndTypeVariables.ts, 3, 7))
>P : Symbol(P, Decl(nonPrimitiveAndTypeVariables.ts, 3, 18))
>V : Symbol(V, Decl(nonPrimitiveAndTypeVariables.ts, 3, 9))

type a = A<{ a: 0 | 1 }, 0>; // { a: 0; }
>a : Symbol(a, Decl(nonPrimitiveAndTypeVariables.ts, 3, 68))
>A : Symbol(A, Decl(nonPrimitiveAndTypeVariables.ts, 0, 0))
>a : Symbol(a, Decl(nonPrimitiveAndTypeVariables.ts, 5, 12))

type b = B<{ a: 0 | 1 }, 0>; // { a: 0; }
>b : Symbol(b, Decl(nonPrimitiveAndTypeVariables.ts, 5, 28))
>B : Symbol(B, Decl(nonPrimitiveAndTypeVariables.ts, 2, 59))
>a : Symbol(a, Decl(nonPrimitiveAndTypeVariables.ts, 6, 12))

function foo<T, U>(x: T) {
>foo : Symbol(foo, Decl(nonPrimitiveAndTypeVariables.ts, 6, 28))
>T : Symbol(T, Decl(nonPrimitiveAndTypeVariables.ts, 8, 13))
>U : Symbol(U, Decl(nonPrimitiveAndTypeVariables.ts, 8, 15))
>x : Symbol(x, Decl(nonPrimitiveAndTypeVariables.ts, 8, 19))
>T : Symbol(T, Decl(nonPrimitiveAndTypeVariables.ts, 8, 13))

let a: object = x; // Error
>a : Symbol(a, Decl(nonPrimitiveAndTypeVariables.ts, 9, 7))
>x : Symbol(x, Decl(nonPrimitiveAndTypeVariables.ts, 8, 19))

let b: U | object = x; // Error
>b : Symbol(b, Decl(nonPrimitiveAndTypeVariables.ts, 10, 7))
>U : Symbol(U, Decl(nonPrimitiveAndTypeVariables.ts, 8, 15))
>x : Symbol(x, Decl(nonPrimitiveAndTypeVariables.ts, 8, 19))
}

50 changes: 50 additions & 0 deletions tests/baselines/reference/nonPrimitiveAndTypeVariables.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
=== tests/cases/conformance/types/nonPrimitive/nonPrimitiveAndTypeVariables.ts ===
// Repros from #23800

type A<T, V> = { [P in keyof T]: T[P] extends V ? 1 : 0; };
>A : A<T, V>
>T : T
>V : V
>P : P
>T : T
>T : T
>P : P
>V : V

type B<T, V> = { [P in keyof T]: T[P] extends V | object ? 1 : 0; };
>B : B<T, V>
>T : T
>V : V
>P : P
>T : T
>T : T
>P : P
>V : V

type a = A<{ a: 0 | 1 }, 0>; // { a: 0; }
>a : A<{ a: 0 | 1; }, 0>
>A : A<T, V>
>a : 0 | 1

type b = B<{ a: 0 | 1 }, 0>; // { a: 0; }
>b : B<{ a: 0 | 1; }, 0>
>B : B<T, V>
>a : 0 | 1

function foo<T, U>(x: T) {
>foo : <T, U>(x: T) => void
>T : T
>U : U
>x : T
>T : T

let a: object = x; // Error
>a : object
>x : T

let b: U | object = x; // Error
>b : object | U
>U : U
>x : T
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// @strict: true

// Repros from #23800

type A<T, V> = { [P in keyof T]: T[P] extends V ? 1 : 0; };
type B<T, V> = { [P in keyof T]: T[P] extends V | object ? 1 : 0; };

type a = A<{ a: 0 | 1 }, 0>; // { a: 0; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the baselines don't actually show that type a = { a: 0 } (or that type b = { a: 0 }.

type b = B<{ a: 0 | 1 }, 0>; // { a: 0; }

function foo<T, U>(x: T) {
let a: object = x; // Error
let b: U | object = x; // Error
}