Skip to content

Propagate the error any type in union and intersection construction #58610

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 3 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 5 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17052,6 +17052,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (flags & TypeFlags.Instantiable) includes |= TypeFlags.IncludesInstantiable;
if (flags & TypeFlags.Intersection && getObjectFlags(type) & ObjectFlags.IsConstrainedTypeVariable) includes |= TypeFlags.IncludesConstrainedTypeVariable;
if (type === wildcardType) includes |= TypeFlags.IncludesWildcard;
if (isErrorType(type)) includes |= TypeFlags.IncludesError;
if (!strictNullChecks && flags & TypeFlags.Nullable) {
if (!(getObjectFlags(type) & ObjectFlags.ContainsWideningType)) includes |= TypeFlags.IncludesNonWideningType;
}
Expand Down Expand Up @@ -17303,7 +17304,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (unionReduction !== UnionReduction.None) {
if (includes & TypeFlags.AnyOrUnknown) {
return includes & TypeFlags.Any ?
includes & TypeFlags.IncludesWildcard ? wildcardType : anyType :
includes & TypeFlags.IncludesWildcard ? wildcardType :
includes & TypeFlags.IncludesError ? errorType : anyType :
unknownType;
Copy link
Member

Choose a reason for hiding this comment

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

These ternaries are officially off the chain now.

Copy link
Member Author

Choose a reason for hiding this comment

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

🤷‍♀️ I just write the ternaries, I let the formatter figure out how it wants to indent them.

}
if (includes & TypeFlags.Undefined) {
Expand Down Expand Up @@ -17445,6 +17447,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
else {
if (flags & TypeFlags.AnyOrUnknown) {
if (type === wildcardType) includes |= TypeFlags.IncludesWildcard;
if (isErrorType(type)) includes |= TypeFlags.IncludesError;
}
else if (strictNullChecks || !(flags & TypeFlags.Nullable)) {
if (type === missingType) {
Expand Down Expand Up @@ -17637,7 +17640,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return neverType;
}
if (includes & TypeFlags.Any) {
return includes & TypeFlags.IncludesWildcard ? wildcardType : anyType;
return includes & TypeFlags.IncludesWildcard ? wildcardType : includes & TypeFlags.IncludesError ? errorType : anyType;
Copy link
Member

Choose a reason for hiding this comment

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

it would be nice to have multiple lines here

}
if (!strictNullChecks && includes & TypeFlags.Nullable) {
return includes & TypeFlags.IncludesEmptyObject ? neverType : includes & TypeFlags.Undefined ? undefinedType : nullType;
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6185,6 +6185,8 @@ export const enum TypeFlags {
StringMapping = 1 << 28, // Uppercase/Lowercase type
/** @internal */
Reserved1 = 1 << 29, // Used by union/intersection type construction
/** @internal */
Reserved2 = 1 << 30, // Used by union/intersection type construction
Copy link
Member

Choose a reason for hiding this comment

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

remind me, do we have 31 bits available?

Copy link
Member

Choose a reason for hiding this comment

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

In modern Node, yep, we do.


/** @internal */
AnyOrUnknown = Any | Unknown,
Expand Down Expand Up @@ -6247,6 +6249,8 @@ export const enum TypeFlags {
/** @internal */
IncludesConstrainedTypeVariable = Reserved1,
/** @internal */
IncludesError = Reserved2,
/** @internal */
NotPrimitiveUnion = Any | Unknown | Void | Never | Object | Intersection | IncludesInstantiable,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function foo() {
>o : any

return (o == null) ? create(base) : defineProperties(Object(o), descriptors);
>(o == null) ? create(base) : defineProperties(Object(o), descriptors) : any
>(o == null) ? create(base) : defineProperties(Object(o), descriptors) : error
>(o == null) : boolean
> : ^^^^^^^
>o == null : boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const a = (
> : ^^^

{0 ? (
>0 ? ( emptyMessage // must be identifier? ) : ( // must be exactly two expression holes <span> {0}{0} </span> ) : any
>0 ? ( emptyMessage // must be identifier? ) : ( // must be exactly two expression holes <span> {0}{0} </span> ) : error
>0 : 0
> : ^
>( emptyMessage // must be identifier? ) : any
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const oobj = {
}

obj[incr()] ??= incr();
>obj[incr()] ??= incr() : any
>obj[incr()] ??= incr() : error
>obj[incr()] : error
>obj : {}
> : ^^
Expand All @@ -50,7 +50,7 @@ obj[incr()] ??= incr();
> : ^^^^^^^^^^^^

oobj["obj"][incr()] ??= incr();
>oobj["obj"][incr()] ??= incr() : any
>oobj["obj"][incr()] ??= incr() : error
>oobj["obj"][incr()] : error
>oobj["obj"] : {}
> : ^^
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const oobj = {
}

obj[incr()] ??= incr();
>obj[incr()] ??= incr() : any
>obj[incr()] ??= incr() : error
>obj[incr()] : error
>obj : {}
> : ^^
Expand All @@ -50,7 +50,7 @@ obj[incr()] ??= incr();
> : ^^^^^^^^^^^^

oobj["obj"][incr()] ??= incr();
>oobj["obj"][incr()] ??= incr() : any
>oobj["obj"][incr()] ??= incr() : error
>oobj["obj"][incr()] : error
>oobj["obj"] : {}
> : ^^
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const oobj = {
}

obj[incr()] ??= incr();
>obj[incr()] ??= incr() : any
>obj[incr()] ??= incr() : error
>obj[incr()] : error
>obj : {}
> : ^^
Expand All @@ -50,7 +50,7 @@ obj[incr()] ??= incr();
> : ^^^^^^^^^^^^

oobj["obj"][incr()] ??= incr();
>oobj["obj"][incr()] ??= incr() : any
>oobj["obj"][incr()] ??= incr() : error
>oobj["obj"][incr()] : error
>oobj["obj"] : {}
> : ^^
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const oobj = {
}

obj[incr()] ??= incr();
>obj[incr()] ??= incr() : any
>obj[incr()] ??= incr() : error
>obj[incr()] : error
>obj : {}
> : ^^
Expand All @@ -50,7 +50,7 @@ obj[incr()] ??= incr();
> : ^^^^^^^^^^^^

oobj["obj"][incr()] ??= incr();
>oobj["obj"][incr()] ??= incr() : any
>oobj["obj"][incr()] ??= incr() : error
>oobj["obj"][incr()] : error
>oobj["obj"] : {}
> : ^^
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ function Y(e, t) {
> : ^^^^^^
>v = f, (0 | (f = f + 288 | 0)) >= (0 | l) && b(288), T = v, A = t : any
>v = f, (0 | (f = f + 288 | 0)) >= (0 | l) && b(288), T = v : error
>v = f, (0 | (f = f + 288 | 0)) >= (0 | l) && b(288) : any
>v = f, (0 | (f = f + 288 | 0)) >= (0 | l) && b(288) : error
>v = f : error
>v : any
>f : error
>(0 | (f = f + 288 | 0)) >= (0 | l) && b(288) : any
>(0 | (f = f + 288 | 0)) >= (0 | l) && b(288) : error
>(0 | (f = f + 288 | 0)) >= (0 | l) : boolean
> : ^^^^^^^
>(0 | (f = f + 288 | 0)) : number
Expand Down Expand Up @@ -298,11 +298,11 @@ function Y(e, t) {
> : ^^^^^^
>s = f, (0 | (f = f + 32 | 0)) >= (0 | l) && b(32), i = e, a = t : any
>s = f, (0 | (f = f + 32 | 0)) >= (0 | l) && b(32), i = e : error
>s = f, (0 | (f = f + 32 | 0)) >= (0 | l) && b(32) : any
>s = f, (0 | (f = f + 32 | 0)) >= (0 | l) && b(32) : error
>s = f : error
>s : any
>f : error
>(0 | (f = f + 32 | 0)) >= (0 | l) && b(32) : any
>(0 | (f = f + 32 | 0)) >= (0 | l) && b(32) : error
>(0 | (f = f + 32 | 0)) >= (0 | l) : boolean
> : ^^^^^^^
>(0 | (f = f + 32 | 0)) : number
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//// [declarationSelfReferentialConstraint.ts] ////
export const object = {
foo: <T extends Set<T> | []>(): void => { },
};
//// [declarationSelfReferentialConstraint.d.ts] ////
export declare const object: {
foo: <T extends Set<T> | []>() => void;
};
2 changes: 1 addition & 1 deletion tests/baselines/reference/tsxReactEmitNesting.types
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ let render = (ctrl, model) =>
> : ^^^^^^

{(!todo.editable) ?
>(!todo.editable) ? <input class="toggle" type="checkbox"></input> : null : any
>(!todo.editable) ? <input class="toggle" type="checkbox"></input> : null : error
>(!todo.editable) : boolean
> : ^^^^^^^
>!todo.editable : boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ function f1(c1: Function, c2: () => object, callable: typeof c1 | typeof c2) {
> : ^^^^^^^^^^^^

const c = callable();
>c : any
>callable() : any
>c : error
>callable() : error
>callable : Function | (() => object)
> : ^^^^^^^^^^^^^^^^^^^^^^^^^
}
Expand All @@ -43,8 +43,8 @@ function f2(fetcherParams: object | (() => object)) {
> : ^^^^^^^^^^^^^^^^ ^

const data = typeof fetcherParams === 'function'
>data : any
>typeof fetcherParams === 'function' ? fetcherParams() : fetcherParams : any
>data : error
>typeof fetcherParams === 'function' ? fetcherParams() : fetcherParams : error
>typeof fetcherParams === 'function' : boolean
> : ^^^^^^^
>typeof fetcherParams : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
Expand All @@ -55,7 +55,7 @@ function f2(fetcherParams: object | (() => object)) {
> : ^^^^^^^^^^

? fetcherParams()
>fetcherParams() : any
>fetcherParams() : error
>fetcherParams : Function | (() => object)
> : ^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
5 changes: 5 additions & 0 deletions tests/cases/transpile/declarationSelfReferentialConstraint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// @declaration: true
// @emitDeclarationOnly: true
export const object = {
foo: <T extends Set<T> | []>(): void => { },
};