Skip to content

Commit b3f3bb3

Browse files
Propagate the error any type in union and intersection construction (#58610)
Co-authored-by: TypeScript Bot <[email protected]>
1 parent a203ace commit b3f3bb3

13 files changed

+42
-22
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17057,6 +17057,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1705717057
if (flags & TypeFlags.Instantiable) includes |= TypeFlags.IncludesInstantiable;
1705817058
if (flags & TypeFlags.Intersection && getObjectFlags(type) & ObjectFlags.IsConstrainedTypeVariable) includes |= TypeFlags.IncludesConstrainedTypeVariable;
1705917059
if (type === wildcardType) includes |= TypeFlags.IncludesWildcard;
17060+
if (isErrorType(type)) includes |= TypeFlags.IncludesError;
1706017061
if (!strictNullChecks && flags & TypeFlags.Nullable) {
1706117062
if (!(getObjectFlags(type) & ObjectFlags.ContainsWideningType)) includes |= TypeFlags.IncludesNonWideningType;
1706217063
}
@@ -17308,7 +17309,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1730817309
if (unionReduction !== UnionReduction.None) {
1730917310
if (includes & TypeFlags.AnyOrUnknown) {
1731017311
return includes & TypeFlags.Any ?
17311-
includes & TypeFlags.IncludesWildcard ? wildcardType : anyType :
17312+
includes & TypeFlags.IncludesWildcard ? wildcardType :
17313+
includes & TypeFlags.IncludesError ? errorType : anyType :
1731217314
unknownType;
1731317315
}
1731417316
if (includes & TypeFlags.Undefined) {
@@ -17450,6 +17452,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1745017452
else {
1745117453
if (flags & TypeFlags.AnyOrUnknown) {
1745217454
if (type === wildcardType) includes |= TypeFlags.IncludesWildcard;
17455+
if (isErrorType(type)) includes |= TypeFlags.IncludesError;
1745317456
}
1745417457
else if (strictNullChecks || !(flags & TypeFlags.Nullable)) {
1745517458
if (type === missingType) {
@@ -17642,7 +17645,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1764217645
return neverType;
1764317646
}
1764417647
if (includes & TypeFlags.Any) {
17645-
return includes & TypeFlags.IncludesWildcard ? wildcardType : anyType;
17648+
return includes & TypeFlags.IncludesWildcard ? wildcardType : includes & TypeFlags.IncludesError ? errorType : anyType;
1764617649
}
1764717650
if (!strictNullChecks && includes & TypeFlags.Nullable) {
1764817651
return includes & TypeFlags.IncludesEmptyObject ? neverType : includes & TypeFlags.Undefined ? undefinedType : nullType;

src/compiler/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6184,6 +6184,8 @@ export const enum TypeFlags {
61846184
StringMapping = 1 << 28, // Uppercase/Lowercase type
61856185
/** @internal */
61866186
Reserved1 = 1 << 29, // Used by union/intersection type construction
6187+
/** @internal */
6188+
Reserved2 = 1 << 30, // Used by union/intersection type construction
61876189

61886190
/** @internal */
61896191
AnyOrUnknown = Any | Unknown,
@@ -6246,6 +6248,8 @@ export const enum TypeFlags {
62466248
/** @internal */
62476249
IncludesConstrainedTypeVariable = Reserved1,
62486250
/** @internal */
6251+
IncludesError = Reserved2,
6252+
/** @internal */
62496253
NotPrimitiveUnion = Any | Unknown | Void | Never | Object | Intersection | IncludesInstantiable,
62506254
}
62516255

tests/baselines/reference/jsDeclarationsExportDoubleAssignmentInClosure.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function foo() {
2323
>o : any
2424

2525
return (o == null) ? create(base) : defineProperties(Object(o), descriptors);
26-
>(o == null) ? create(base) : defineProperties(Object(o), descriptors) : any
26+
>(o == null) ? create(base) : defineProperties(Object(o), descriptors) : error
2727
>(o == null) : boolean
2828
> : ^^^^^^^
2929
>o == null : boolean

tests/baselines/reference/jsxNestedWithinTernaryParsesCorrectly.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const a = (
1515
> : ^^^
1616

1717
{0 ? (
18-
>0 ? ( emptyMessage // must be identifier? ) : ( // must be exactly two expression holes <span> {0}{0} </span> ) : any
18+
>0 ? ( emptyMessage // must be identifier? ) : ( // must be exactly two expression holes <span> {0}{0} </span> ) : error
1919
>0 : 0
2020
> : ^
2121
>( emptyMessage // must be identifier? ) : any

tests/baselines/reference/logicalAssignment10(target=es2015).types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const oobj = {
3636
}
3737

3838
obj[incr()] ??= incr();
39-
>obj[incr()] ??= incr() : any
39+
>obj[incr()] ??= incr() : error
4040
>obj[incr()] : error
4141
>obj : {}
4242
> : ^^
@@ -50,7 +50,7 @@ obj[incr()] ??= incr();
5050
> : ^^^^^^^^^^^^
5151

5252
oobj["obj"][incr()] ??= incr();
53-
>oobj["obj"][incr()] ??= incr() : any
53+
>oobj["obj"][incr()] ??= incr() : error
5454
>oobj["obj"][incr()] : error
5555
>oobj["obj"] : {}
5656
> : ^^

tests/baselines/reference/logicalAssignment10(target=es2020).types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const oobj = {
3636
}
3737

3838
obj[incr()] ??= incr();
39-
>obj[incr()] ??= incr() : any
39+
>obj[incr()] ??= incr() : error
4040
>obj[incr()] : error
4141
>obj : {}
4242
> : ^^
@@ -50,7 +50,7 @@ obj[incr()] ??= incr();
5050
> : ^^^^^^^^^^^^
5151

5252
oobj["obj"][incr()] ??= incr();
53-
>oobj["obj"][incr()] ??= incr() : any
53+
>oobj["obj"][incr()] ??= incr() : error
5454
>oobj["obj"][incr()] : error
5555
>oobj["obj"] : {}
5656
> : ^^

tests/baselines/reference/logicalAssignment10(target=es2021).types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const oobj = {
3636
}
3737

3838
obj[incr()] ??= incr();
39-
>obj[incr()] ??= incr() : any
39+
>obj[incr()] ??= incr() : error
4040
>obj[incr()] : error
4141
>obj : {}
4242
> : ^^
@@ -50,7 +50,7 @@ obj[incr()] ??= incr();
5050
> : ^^^^^^^^^^^^
5151

5252
oobj["obj"][incr()] ??= incr();
53-
>oobj["obj"][incr()] ??= incr() : any
53+
>oobj["obj"][incr()] ??= incr() : error
5454
>oobj["obj"][incr()] : error
5555
>oobj["obj"] : {}
5656
> : ^^

tests/baselines/reference/logicalAssignment10(target=esnext).types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const oobj = {
3636
}
3737

3838
obj[incr()] ??= incr();
39-
>obj[incr()] ??= incr() : any
39+
>obj[incr()] ??= incr() : error
4040
>obj[incr()] : error
4141
>obj : {}
4242
> : ^^
@@ -50,7 +50,7 @@ obj[incr()] ??= incr();
5050
> : ^^^^^^^^^^^^
5151

5252
oobj["obj"][incr()] ??= incr();
53-
>oobj["obj"][incr()] ??= incr() : any
53+
>oobj["obj"][incr()] ??= incr() : error
5454
>oobj["obj"][incr()] : error
5555
>oobj["obj"] : {}
5656
> : ^^

tests/baselines/reference/parsingDeepParenthensizedExpression.types

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,11 @@ function Y(e, t) {
101101
> : ^^^^^^
102102
>v = f, (0 | (f = f + 288 | 0)) >= (0 | l) && b(288), T = v, A = t : any
103103
>v = f, (0 | (f = f + 288 | 0)) >= (0 | l) && b(288), T = v : error
104-
>v = f, (0 | (f = f + 288 | 0)) >= (0 | l) && b(288) : any
104+
>v = f, (0 | (f = f + 288 | 0)) >= (0 | l) && b(288) : error
105105
>v = f : error
106106
>v : any
107107
>f : error
108-
>(0 | (f = f + 288 | 0)) >= (0 | l) && b(288) : any
108+
>(0 | (f = f + 288 | 0)) >= (0 | l) && b(288) : error
109109
>(0 | (f = f + 288 | 0)) >= (0 | l) : boolean
110110
> : ^^^^^^^
111111
>(0 | (f = f + 288 | 0)) : number
@@ -298,11 +298,11 @@ function Y(e, t) {
298298
> : ^^^^^^
299299
>s = f, (0 | (f = f + 32 | 0)) >= (0 | l) && b(32), i = e, a = t : any
300300
>s = f, (0 | (f = f + 32 | 0)) >= (0 | l) && b(32), i = e : error
301-
>s = f, (0 | (f = f + 32 | 0)) >= (0 | l) && b(32) : any
301+
>s = f, (0 | (f = f + 32 | 0)) >= (0 | l) && b(32) : error
302302
>s = f : error
303303
>s : any
304304
>f : error
305-
>(0 | (f = f + 32 | 0)) >= (0 | l) && b(32) : any
305+
>(0 | (f = f + 32 | 0)) >= (0 | l) && b(32) : error
306306
>(0 | (f = f + 32 | 0)) >= (0 | l) : boolean
307307
> : ^^^^^^^
308308
>(0 | (f = f + 32 | 0)) : number
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//// [declarationSelfReferentialConstraint.ts] ////
2+
export const object = {
3+
foo: <T extends Set<T> | []>(): void => { },
4+
};
5+
//// [declarationSelfReferentialConstraint.d.ts] ////
6+
export declare const object: {
7+
foo: <T extends Set<T> | []>() => void;
8+
};

tests/baselines/reference/tsxReactEmitNesting.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ let render = (ctrl, model) =>
191191
> : ^^^^^^
192192

193193
{(!todo.editable) ?
194-
>(!todo.editable) ? <input class="toggle" type="checkbox"></input> : null : any
194+
>(!todo.editable) ? <input class="toggle" type="checkbox"></input> : null : error
195195
>(!todo.editable) : boolean
196196
> : ^^^^^^^
197197
>!todo.editable : boolean

tests/baselines/reference/unionOfFunctionAndSignatureIsCallable.types

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ function f1(c1: Function, c2: () => object, callable: typeof c1 | typeof c2) {
3030
> : ^^^^^^^^^^^^
3131

3232
const c = callable();
33-
>c : any
34-
>callable() : any
33+
>c : error
34+
>callable() : error
3535
>callable : Function | (() => object)
3636
> : ^^^^^^^^^^^^^^^^^^^^^^^^^
3737
}
@@ -43,8 +43,8 @@ function f2(fetcherParams: object | (() => object)) {
4343
> : ^^^^^^^^^^^^^^^^ ^
4444

4545
const data = typeof fetcherParams === 'function'
46-
>data : any
47-
>typeof fetcherParams === 'function' ? fetcherParams() : fetcherParams : any
46+
>data : error
47+
>typeof fetcherParams === 'function' ? fetcherParams() : fetcherParams : error
4848
>typeof fetcherParams === 'function' : boolean
4949
> : ^^^^^^^
5050
>typeof fetcherParams : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
@@ -55,7 +55,7 @@ function f2(fetcherParams: object | (() => object)) {
5555
> : ^^^^^^^^^^
5656

5757
? fetcherParams()
58-
>fetcherParams() : any
58+
>fetcherParams() : error
5959
>fetcherParams : Function | (() => object)
6060
> : ^^^^^^^^^^^^^^^^^^^^^^^^^
6161

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// @declaration: true
2+
// @emitDeclarationOnly: true
3+
export const object = {
4+
foo: <T extends Set<T> | []>(): void => { },
5+
};

0 commit comments

Comments
 (0)