Skip to content

Commit e0efcc4

Browse files
committed
Try making the apparent type of unknown be unknown
1 parent ad5ca67 commit e0efcc4

17 files changed

+301
-87
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12076,7 +12076,7 @@ namespace ts {
1207612076
t.flags & TypeFlags.ESSymbolLike ? getGlobalESSymbolType(/*reportErrors*/ languageVersion >= ScriptTarget.ES2015) :
1207712077
t.flags & TypeFlags.NonPrimitive ? emptyObjectType :
1207812078
t.flags & TypeFlags.Index ? keyofConstraintType :
12079-
t.flags & TypeFlags.Unknown && !strictNullChecks ? emptyObjectType :
12079+
// t.flags & TypeFlags.Unknown && !strictNullChecks ? emptyObjectType :
1208012080
t;
1208112081
}
1208212082

tests/baselines/reference/genericConditionalConstrainedToUnknownNotAssignableToConcreteObject.errors.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ tests/cases/compiler/genericConditionalConstrainedToUnknownNotAssignableToConcre
99
Type 'unknown' is not assignable to type 'A'.
1010
Type 'ReturnType<FunctionsObj<T>[string]>' is not assignable to type 'A'.
1111
Type 'unknown' is not assignable to type 'A'.
12-
Property 'x' is missing in type '{}' but required in type 'A'.
12+
Type 'unknown' is not assignable to type 'A'.
1313

1414

1515
==== tests/cases/compiler/genericConditionalConstrainedToUnknownNotAssignableToConcreteObject.ts (1 errors) ====
@@ -38,8 +38,7 @@ tests/cases/compiler/genericConditionalConstrainedToUnknownNotAssignableToConcre
3838
!!! error TS2322: Type 'unknown' is not assignable to type 'A'.
3939
!!! error TS2322: Type 'ReturnType<FunctionsObj<T>[string]>' is not assignable to type 'A'.
4040
!!! error TS2322: Type 'unknown' is not assignable to type 'A'.
41-
!!! error TS2322: Property 'x' is missing in type '{}' but required in type 'A'.
42-
!!! related TS2728 tests/cases/compiler/genericConditionalConstrainedToUnknownNotAssignableToConcreteObject.ts:1:15: 'x' is declared here.
41+
!!! error TS2322: Type 'unknown' is not assignable to type 'A'.
4342
}
4443

4544
// Original CFA report of the above issue

tests/baselines/reference/privateFieldAssignabilityFromUnknown.errors.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
tests/cases/compiler/privateFieldAssignabilityFromUnknown.ts(2,3): error TS18028: Private identifiers are only available when targeting ECMAScript 2015 and higher.
2-
tests/cases/compiler/privateFieldAssignabilityFromUnknown.ts(5,7): error TS2741: Property '#field' is missing in type '{}' but required in type 'Class'.
2+
tests/cases/compiler/privateFieldAssignabilityFromUnknown.ts(5,7): error TS2322: Type 'unknown' is not assignable to type 'Class'.
33

44

55
==== tests/cases/compiler/privateFieldAssignabilityFromUnknown.ts (2 errors) ====
@@ -11,6 +11,5 @@ tests/cases/compiler/privateFieldAssignabilityFromUnknown.ts(5,7): error TS2741:
1111

1212
const task: Class = {} as unknown;
1313
~~~~
14-
!!! error TS2741: Property '#field' is missing in type '{}' but required in type 'Class'.
15-
!!! related TS2728 tests/cases/compiler/privateFieldAssignabilityFromUnknown.ts:2:3: '#field' is declared here.
14+
!!! error TS2322: Type 'unknown' is not assignable to type 'Class'.
1615

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
tests/cases/conformance/types/typeParameters/typeParameterLists/propertyAccessOnTypeParameterWithoutConstraints.ts(5,22): error TS2339: Property 'toString' does not exist on type 'T'.
2+
tests/cases/conformance/types/typeParameters/typeParameterLists/propertyAccessOnTypeParameterWithoutConstraints.ts(21,22): error TS2339: Property 'toString' does not exist on type 'unknown'.
3+
tests/cases/conformance/types/typeParameters/typeParameterLists/propertyAccessOnTypeParameterWithoutConstraints.ts(27,22): error TS2339: Property 'toString' does not exist on type 'T'.
4+
5+
6+
==== tests/cases/conformance/types/typeParameters/typeParameterLists/propertyAccessOnTypeParameterWithoutConstraints.ts (3 errors) ====
7+
class C<T> {
8+
f() {
9+
var x: T;
10+
var a = x['toString'](); // should be string
11+
return a + x.toString();
12+
~~~~~~~~
13+
!!! error TS2339: Property 'toString' does not exist on type 'T'.
14+
}
15+
}
16+
17+
var r = (new C<number>()).f();
18+
19+
interface I<T> {
20+
foo: T;
21+
}
22+
var i: I<number>;
23+
var r2 = i.foo.toString();
24+
var r2b = i.foo['toString']();
25+
26+
var a: {
27+
<T>(): T;
28+
}
29+
var r3: string = a().toString();
30+
~~~~~~~~
31+
!!! error TS2339: Property 'toString' does not exist on type 'unknown'.
32+
var r3b: string = a()['toString']();
33+
34+
var b = {
35+
foo: <T>(x: T) => {
36+
var a = x['toString'](); // should be string
37+
return a + x.toString();
38+
~~~~~~~~
39+
!!! error TS2339: Property 'toString' does not exist on type 'T'.
40+
}
41+
}
42+
43+
var r4 = b.foo(1);

tests/baselines/reference/propertyAccessOnTypeParameterWithoutConstraints.symbols

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,10 @@ class C<T> {
1313
var a = x['toString'](); // should be string
1414
>a : Symbol(a, Decl(propertyAccessOnTypeParameterWithoutConstraints.ts, 3, 11))
1515
>x : Symbol(x, Decl(propertyAccessOnTypeParameterWithoutConstraints.ts, 2, 11))
16-
>'toString' : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --))
1716

1817
return a + x.toString();
1918
>a : Symbol(a, Decl(propertyAccessOnTypeParameterWithoutConstraints.ts, 3, 11))
20-
>x.toString : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --))
2119
>x : Symbol(x, Decl(propertyAccessOnTypeParameterWithoutConstraints.ts, 2, 11))
22-
>toString : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --))
2320
}
2421
}
2522

@@ -65,14 +62,11 @@ var a: {
6562
}
6663
var r3: string = a().toString();
6764
>r3 : Symbol(r3, Decl(propertyAccessOnTypeParameterWithoutConstraints.ts, 20, 3))
68-
>a().toString : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --))
6965
>a : Symbol(a, Decl(propertyAccessOnTypeParameterWithoutConstraints.ts, 17, 3))
70-
>toString : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --))
7166

7267
var r3b: string = a()['toString']();
7368
>r3b : Symbol(r3b, Decl(propertyAccessOnTypeParameterWithoutConstraints.ts, 21, 3))
7469
>a : Symbol(a, Decl(propertyAccessOnTypeParameterWithoutConstraints.ts, 17, 3))
75-
>'toString' : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --))
7670

7771
var b = {
7872
>b : Symbol(b, Decl(propertyAccessOnTypeParameterWithoutConstraints.ts, 23, 3))
@@ -86,13 +80,10 @@ var b = {
8680
var a = x['toString'](); // should be string
8781
>a : Symbol(a, Decl(propertyAccessOnTypeParameterWithoutConstraints.ts, 25, 11))
8882
>x : Symbol(x, Decl(propertyAccessOnTypeParameterWithoutConstraints.ts, 24, 13))
89-
>'toString' : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --))
9083

9184
return a + x.toString();
9285
>a : Symbol(a, Decl(propertyAccessOnTypeParameterWithoutConstraints.ts, 25, 11))
93-
>x.toString : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --))
9486
>x : Symbol(x, Decl(propertyAccessOnTypeParameterWithoutConstraints.ts, 24, 13))
95-
>toString : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --))
9687
}
9788
}
9889

tests/baselines/reference/propertyAccessOnTypeParameterWithoutConstraints.types

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,36 @@ class C<T> {
33
>C : C<T>
44

55
f() {
6-
>f : () => string
6+
>f : () => any
77

88
var x: T;
99
>x : T
1010

1111
var a = x['toString'](); // should be string
12-
>a : string
13-
>x['toString']() : string
14-
>x['toString'] : () => string
12+
>a : any
13+
>x['toString']() : any
14+
>x['toString'] : any
1515
>x : T
1616
>'toString' : "toString"
1717

1818
return a + x.toString();
19-
>a + x.toString() : string
20-
>a : string
21-
>x.toString() : string
22-
>x.toString : () => string
19+
>a + x.toString() : any
20+
>a : any
21+
>x.toString() : any
22+
>x.toString : any
2323
>x : T
24-
>toString : () => string
24+
>toString : any
2525
}
2626
}
2727

2828
var r = (new C<number>()).f();
29-
>r : string
30-
>(new C<number>()).f() : string
31-
>(new C<number>()).f : () => string
29+
>r : any
30+
>(new C<number>()).f() : any
31+
>(new C<number>()).f : () => any
3232
>(new C<number>()) : C<number>
3333
>new C<number>() : C<number>
3434
>C : typeof C
35-
>f : () => string
35+
>f : () => any
3636

3737
interface I<T> {
3838
foo: T;
@@ -66,51 +66,51 @@ var a: {
6666
}
6767
var r3: string = a().toString();
6868
>r3 : string
69-
>a().toString() : string
70-
>a().toString : () => string
69+
>a().toString() : any
70+
>a().toString : any
7171
>a() : unknown
7272
>a : <T>() => T
73-
>toString : () => string
73+
>toString : any
7474

7575
var r3b: string = a()['toString']();
7676
>r3b : string
77-
>a()['toString']() : string
78-
>a()['toString'] : () => string
77+
>a()['toString']() : any
78+
>a()['toString'] : any
7979
>a() : unknown
8080
>a : <T>() => T
8181
>'toString' : "toString"
8282

8383
var b = {
84-
>b : { foo: <T>(x: T) => string; }
85-
>{ foo: <T>(x: T) => { var a = x['toString'](); // should be string return a + x.toString(); }} : { foo: <T>(x: T) => string; }
84+
>b : { foo: <T>(x: T) => any; }
85+
>{ foo: <T>(x: T) => { var a = x['toString'](); // should be string return a + x.toString(); }} : { foo: <T>(x: T) => any; }
8686

8787
foo: <T>(x: T) => {
88-
>foo : <T>(x: T) => string
89-
><T>(x: T) => { var a = x['toString'](); // should be string return a + x.toString(); } : <T>(x: T) => string
88+
>foo : <T>(x: T) => any
89+
><T>(x: T) => { var a = x['toString'](); // should be string return a + x.toString(); } : <T>(x: T) => any
9090
>x : T
9191

9292
var a = x['toString'](); // should be string
93-
>a : string
94-
>x['toString']() : string
95-
>x['toString'] : () => string
93+
>a : any
94+
>x['toString']() : any
95+
>x['toString'] : any
9696
>x : T
9797
>'toString' : "toString"
9898

9999
return a + x.toString();
100-
>a + x.toString() : string
101-
>a : string
102-
>x.toString() : string
103-
>x.toString : () => string
100+
>a + x.toString() : any
101+
>a : any
102+
>x.toString() : any
103+
>x.toString : any
104104
>x : T
105-
>toString : () => string
105+
>toString : any
106106
}
107107
}
108108

109109
var r4 = b.foo(1);
110-
>r4 : string
111-
>b.foo(1) : string
112-
>b.foo : <T>(x: T) => string
113-
>b : { foo: <T>(x: T) => string; }
114-
>foo : <T>(x: T) => string
110+
>r4 : any
111+
>b.foo(1) : any
112+
>b.foo : <T>(x: T) => any
113+
>b : { foo: <T>(x: T) => any; }
114+
>foo : <T>(x: T) => any
115115
>1 : 1
116116

0 commit comments

Comments
 (0)