Skip to content
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

Fixed an issue with in not being able to be used on narrowed down expression of a generic nullable type #51502

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34196,7 +34196,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// that include {} (we know that the other types in such intersections are assignable to object
// since we already checked for that).
if (hasEmptyObjectIntersection(rightType)) {
error(right, Diagnostics.Type_0_may_represent_a_primitive_value_which_is_not_permitted_as_the_right_operand_of_the_in_operator, typeToString(rightType));
const constraint = getBaseConstraintOfType(rightType);
if (!constraint || isEmptyAnonymousObjectType(constraint)) {
error(right, Diagnostics.Type_0_may_represent_a_primitive_value_which_is_not_permitted_as_the_right_operand_of_the_in_operator, typeToString(rightType));
}
Copy link
Member

Choose a reason for hiding this comment

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

Instead of adding these lines of code, I'd suggest modifying hasEmptyObjectIntersection to

    function hasEmptyObjectIntersection(type: Type): boolean {
        return someType(type, t => t === unknownEmptyObjectType || !!(t.flags & TypeFlags.Intersection) && isEmptyAnonymousObjectType(getBaseConstraintOrType(t)));
    }

Accomplishes the same thing with less code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice, I've pushed out the requested change.

}
}
// The result is always of the Boolean primitive type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,4 +420,16 @@ tests/cases/compiler/inKeywordTypeguard.ts(186,21): error TS2322: Type 'T' is no

const checkIsTouchDevice = () =>
"ontouchstart" in window || "msMaxTouchPoints" in window.navigator;

// Repro from #51501

function isHTMLTable<T extends object | null>(table: T): boolean {
return !!table && 'html' in table;
}

// Repro from #51549

const f = <P extends object>(a: P & {}) => {
"foo" in a;
};

20 changes: 20 additions & 0 deletions tests/baselines/reference/inKeywordTypeguard(strict=false).js
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,18 @@ function foo<A>(value: A) {

const checkIsTouchDevice = () =>
"ontouchstart" in window || "msMaxTouchPoints" in window.navigator;

// Repro from #51501

function isHTMLTable<T extends object | null>(table: T): boolean {
return !!table && 'html' in table;
}

// Repro from #51549

const f = <P extends object>(a: P & {}) => {
"foo" in a;
};


//// [inKeywordTypeguard.js]
Expand Down Expand Up @@ -655,3 +667,11 @@ function foo(value) {
}
// Repro from #50954
const checkIsTouchDevice = () => "ontouchstart" in window || "msMaxTouchPoints" in window.navigator;
// Repro from #51501
function isHTMLTable(table) {
return !!table && 'html' in table;
}
// Repro from #51549
const f = (a) => {
"foo" in a;
};
26 changes: 26 additions & 0 deletions tests/baselines/reference/inKeywordTypeguard(strict=false).symbols
Original file line number Diff line number Diff line change
Expand Up @@ -853,3 +853,29 @@ const checkIsTouchDevice = () =>
>window : Symbol(window, Decl(lib.dom.d.ts, --, --))
>navigator : Symbol(navigator, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --))

// Repro from #51501

function isHTMLTable<T extends object | null>(table: T): boolean {
>isHTMLTable : Symbol(isHTMLTable, Decl(inKeywordTypeguard.ts, 341, 71))
>T : Symbol(T, Decl(inKeywordTypeguard.ts, 345, 21))
>table : Symbol(table, Decl(inKeywordTypeguard.ts, 345, 46))
>T : Symbol(T, Decl(inKeywordTypeguard.ts, 345, 21))

return !!table && 'html' in table;
>table : Symbol(table, Decl(inKeywordTypeguard.ts, 345, 46))
>table : Symbol(table, Decl(inKeywordTypeguard.ts, 345, 46))
}

// Repro from #51549

const f = <P extends object>(a: P & {}) => {
>f : Symbol(f, Decl(inKeywordTypeguard.ts, 351, 5))
>P : Symbol(P, Decl(inKeywordTypeguard.ts, 351, 11))
>a : Symbol(a, Decl(inKeywordTypeguard.ts, 351, 29))
>P : Symbol(P, Decl(inKeywordTypeguard.ts, 351, 11))

"foo" in a;
>a : Symbol(a, Decl(inKeywordTypeguard.ts, 351, 29))

};

31 changes: 31 additions & 0 deletions tests/baselines/reference/inKeywordTypeguard(strict=false).types
Original file line number Diff line number Diff line change
Expand Up @@ -1056,3 +1056,34 @@ const checkIsTouchDevice = () =>
>window : Window & typeof globalThis
>navigator : Navigator

// Repro from #51501

function isHTMLTable<T extends object | null>(table: T): boolean {
>isHTMLTable : <T extends object>(table: T) => boolean
>null : null
>table : T

return !!table && 'html' in table;
>!!table && 'html' in table : boolean
>!!table : boolean
>!table : boolean
>table : T
>'html' in table : boolean
>'html' : "html"
>table : T
}

// Repro from #51549

const f = <P extends object>(a: P & {}) => {
>f : <P extends object>(a: P & {}) => void
><P extends object>(a: P & {}) => { "foo" in a;} : <P extends object>(a: P & {}) => void
>a : P & {}

"foo" in a;
>"foo" in a : boolean
>"foo" : "foo"
>a : P & {}

};

Original file line number Diff line number Diff line change
Expand Up @@ -440,4 +440,16 @@ tests/cases/compiler/inKeywordTypeguard.ts(186,21): error TS2638: Type 'NonNulla

const checkIsTouchDevice = () =>
"ontouchstart" in window || "msMaxTouchPoints" in window.navigator;

// Repro from #51501

function isHTMLTable<T extends object | null>(table: T): boolean {
return !!table && 'html' in table;
}

// Repro from #51549

const f = <P extends object>(a: P & {}) => {
"foo" in a;
};

20 changes: 20 additions & 0 deletions tests/baselines/reference/inKeywordTypeguard(strict=true).js
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,18 @@ function foo<A>(value: A) {

const checkIsTouchDevice = () =>
"ontouchstart" in window || "msMaxTouchPoints" in window.navigator;

// Repro from #51501

function isHTMLTable<T extends object | null>(table: T): boolean {
return !!table && 'html' in table;
}

// Repro from #51549

const f = <P extends object>(a: P & {}) => {
"foo" in a;
};


//// [inKeywordTypeguard.js]
Expand Down Expand Up @@ -656,3 +668,11 @@ function foo(value) {
}
// Repro from #50954
const checkIsTouchDevice = () => "ontouchstart" in window || "msMaxTouchPoints" in window.navigator;
// Repro from #51501
function isHTMLTable(table) {
return !!table && 'html' in table;
}
// Repro from #51549
const f = (a) => {
"foo" in a;
};
26 changes: 26 additions & 0 deletions tests/baselines/reference/inKeywordTypeguard(strict=true).symbols
Original file line number Diff line number Diff line change
Expand Up @@ -853,3 +853,29 @@ const checkIsTouchDevice = () =>
>window : Symbol(window, Decl(lib.dom.d.ts, --, --))
>navigator : Symbol(navigator, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --))

// Repro from #51501

function isHTMLTable<T extends object | null>(table: T): boolean {
>isHTMLTable : Symbol(isHTMLTable, Decl(inKeywordTypeguard.ts, 341, 71))
>T : Symbol(T, Decl(inKeywordTypeguard.ts, 345, 21))
>table : Symbol(table, Decl(inKeywordTypeguard.ts, 345, 46))
>T : Symbol(T, Decl(inKeywordTypeguard.ts, 345, 21))

return !!table && 'html' in table;
>table : Symbol(table, Decl(inKeywordTypeguard.ts, 345, 46))
>table : Symbol(table, Decl(inKeywordTypeguard.ts, 345, 46))
}

// Repro from #51549

const f = <P extends object>(a: P & {}) => {
>f : Symbol(f, Decl(inKeywordTypeguard.ts, 351, 5))
>P : Symbol(P, Decl(inKeywordTypeguard.ts, 351, 11))
>a : Symbol(a, Decl(inKeywordTypeguard.ts, 351, 29))
>P : Symbol(P, Decl(inKeywordTypeguard.ts, 351, 11))

"foo" in a;
>a : Symbol(a, Decl(inKeywordTypeguard.ts, 351, 29))

};

31 changes: 31 additions & 0 deletions tests/baselines/reference/inKeywordTypeguard(strict=true).types
Original file line number Diff line number Diff line change
Expand Up @@ -1056,3 +1056,34 @@ const checkIsTouchDevice = () =>
>window : Window & typeof globalThis
>navigator : Navigator

// Repro from #51501

function isHTMLTable<T extends object | null>(table: T): boolean {
>isHTMLTable : <T extends object | null>(table: T) => boolean
>null : null
>table : T

return !!table && 'html' in table;
>!!table && 'html' in table : boolean
>!!table : boolean
>!table : boolean
>table : T
>'html' in table : boolean
>'html' : "html"
>table : NonNullable<T>
}

// Repro from #51549

const f = <P extends object>(a: P & {}) => {
>f : <P extends object>(a: P & {}) => void
><P extends object>(a: P & {}) => { "foo" in a;} : <P extends object>(a: P & {}) => void
>a : P & {}

"foo" in a;
>"foo" in a : boolean
>"foo" : "foo"
>a : P & {}

};

12 changes: 12 additions & 0 deletions tests/cases/compiler/inKeywordTypeguard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,15 @@ function foo<A>(value: A) {

const checkIsTouchDevice = () =>
"ontouchstart" in window || "msMaxTouchPoints" in window.navigator;

// Repro from #51501

function isHTMLTable<T extends object | null>(table: T): boolean {
return !!table && 'html' in table;
}

// Repro from #51549

const f = <P extends object>(a: P & {}) => {
"foo" in a;
};