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

Properly compute SymbolFlags.Optional for intersected properties #50958

Merged
merged 8 commits into from Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
13 changes: 11 additions & 2 deletions src/compiler/checker.ts
Expand Up @@ -12524,7 +12524,10 @@ namespace ts {
optionalFlag |= (prop.flags & SymbolFlags.Optional);
}
else {
optionalFlag &= prop.flags;
// In non-strictNullChecks mode, properties originating in non-member declarations (e.g. module exports
// or globalThis members) are considered optional since `undefined` is implicitly part of the type. This
// matters for narrowing caused by `in` checks (specifically the isTypePresencePossible function).
optionalFlag &= strictNullChecks || prop.flags & (SymbolFlags.Property | SymbolFlags.Method | SymbolFlags.Accessor) ? prop.flags : SymbolFlags.Optional;
}
if (!singleProp) {
singleProp = prop;
Expand Down Expand Up @@ -25254,10 +25257,16 @@ namespace ts {
return type;
}

function isPossiblyUndefined(type: Type) {
// We use TypeFlags.IsUndefined here because TypeFlags.EQUndefined is true for practically all
// types in non-strictNullChecks mode.
return !!(type.flags & TypeFlags.AnyOrUnknown || getTypeFacts(type) & TypeFacts.IsUndefined);
}

function isTypePresencePossible(type: Type, propName: __String, assumeTrue: boolean) {
const prop = getPropertyOfType(type, propName);
return prop ?
!!(prop.flags & SymbolFlags.Optional) || assumeTrue :
!!(prop.flags & SymbolFlags.Optional) || isPossiblyUndefined(getTypeOfSymbol(prop)) || assumeTrue :
!!getApplicableIndexInfoForName(type, propName) || !assumeTrue;
}

Expand Down
Expand Up @@ -21,14 +21,13 @@ tests/cases/compiler/inKeywordTypeguard.ts(74,32): error TS2339: Property 'a' do
tests/cases/compiler/inKeywordTypeguard.ts(82,39): error TS2339: Property 'b' does not exist on type 'A'.
tests/cases/compiler/inKeywordTypeguard.ts(84,39): error TS2339: Property 'a' does not exist on type 'B'.
tests/cases/compiler/inKeywordTypeguard.ts(94,26): error TS2339: Property 'a' does not exist on type 'never'.
tests/cases/compiler/inKeywordTypeguard.ts(150,16): error TS2339: Property 'ontouchstart' does not exist on type 'never'.
tests/cases/compiler/inKeywordTypeguard.ts(155,16): error TS2322: Type 'unknown' is not assignable to type 'object'.
tests/cases/compiler/inKeywordTypeguard.ts(158,21): error TS2322: Type 'unknown' is not assignable to type 'object'.
tests/cases/compiler/inKeywordTypeguard.ts(183,16): error TS2322: Type 'T' is not assignable to type 'object'.
tests/cases/compiler/inKeywordTypeguard.ts(186,21): error TS2322: Type 'T' is not assignable to type 'object'.


==== tests/cases/compiler/inKeywordTypeguard.ts (22 errors) ====
==== tests/cases/compiler/inKeywordTypeguard.ts (21 errors) ====
class A { a: string; }
class B { b: string; }

Expand Down Expand Up @@ -219,8 +218,6 @@ tests/cases/compiler/inKeywordTypeguard.ts(186,21): error TS2322: Type 'T' is no
window.ontouchstart
} else {
window.ontouchstart
~~~~~~~~~~~~
!!! error TS2339: Property 'ontouchstart' does not exist on type 'never'.
}
}

Expand Down Expand Up @@ -353,11 +350,70 @@ tests/cases/compiler/inKeywordTypeguard.ts(186,21): error TS2322: Type 'T' is no
}
}

function f10(x: { a: unknown }) {
if ("a" in x) {
x;
}
else {
x;
}
}

function f11(x: { a: any }) {
if ("a" in x) {
x;
}
else {
x;
}
}

function f12(x: { a: string }) {
if ("a" in x) {
x;
}
else {
x;
}
}

function f13(x: { a?: string }) {
if ("a" in x) {
x;
}
else {
x;
}
}

function f14(x: { a: string | undefined }) {
if ("a" in x) {
x;
}
else {
x;
}
}

function f15(x: { a?: string | undefined }) {
if ("a" in x) {
x;
}
else {
x;
}
}

// Repro from #50639

function foo<A>(value: A) {
if (typeof value === "object" && value !== null && "prop" in value) {
value; // A & object & Record<"prop", unknown>
}
}

// Repro from #50954

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

109 changes: 109 additions & 0 deletions tests/baselines/reference/inKeywordTypeguard(strict=false).js
Expand Up @@ -271,13 +271,72 @@ function f9(x: object) {
}
}

function f10(x: { a: unknown }) {
if ("a" in x) {
x;
}
else {
x;
}
}

function f11(x: { a: any }) {
if ("a" in x) {
x;
}
else {
x;
}
}

function f12(x: { a: string }) {
if ("a" in x) {
x;
}
else {
x;
}
}

function f13(x: { a?: string }) {
if ("a" in x) {
x;
}
else {
x;
}
}

function f14(x: { a: string | undefined }) {
if ("a" in x) {
x;
}
else {
x;
}
}

function f15(x: { a?: string | undefined }) {
if ("a" in x) {
x;
}
else {
x;
}
}

// Repro from #50639

function foo<A>(value: A) {
if (typeof value === "object" && value !== null && "prop" in value) {
value; // A & object & Record<"prop", unknown>
}
}

// Repro from #50954

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


//// [inKeywordTypeguard.js]
Expand Down Expand Up @@ -533,9 +592,59 @@ function f9(x) {
x[sym];
}
}
function f10(x) {
if ("a" in x) {
x;
}
else {
x;
}
}
function f11(x) {
if ("a" in x) {
x;
}
else {
x;
}
}
function f12(x) {
if ("a" in x) {
x;
}
else {
x;
}
}
function f13(x) {
if ("a" in x) {
x;
}
else {
x;
}
}
function f14(x) {
if ("a" in x) {
x;
}
else {
x;
}
}
function f15(x) {
if ("a" in x) {
x;
}
else {
x;
}
}
// Repro from #50639
function foo(value) {
if (typeof value === "object" && value !== null && "prop" in value) {
value; // A & object & Record<"prop", unknown>
}
}
// Repro from #50954
const checkIsTouchDevice = () => "ontouchstart" in window || "msMaxTouchPoints" in window.navigator;