From 316f0ab7f9fd2cf79ddaf2264d3a629c5d433c43 Mon Sep 17 00:00:00 2001 From: Ryan Cavanaugh Date: Wed, 6 Nov 2024 11:43:12 -0800 Subject: [PATCH] MetaProperty is possibly nullish Fixes #60439 --- src/compiler/checker.ts | 1 + .../reference/predicateSemantics.errors.txt | 7 +++++++ .../baselines/reference/predicateSemantics.js | 16 +++++++++++++++ .../reference/predicateSemantics.symbols | 12 +++++++++++ .../reference/predicateSemantics.types | 20 +++++++++++++++++++ tests/cases/compiler/predicateSemantics.ts | 7 +++++++ 6 files changed, 63 insertions(+) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index d1d09f32dd8cd..735d42fefa706 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -39755,6 +39755,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { case SyntaxKind.AwaitExpression: case SyntaxKind.CallExpression: case SyntaxKind.ElementAccessExpression: + case SyntaxKind.MetaProperty: case SyntaxKind.NewExpression: case SyntaxKind.PropertyAccessExpression: case SyntaxKind.YieldExpression: diff --git a/tests/baselines/reference/predicateSemantics.errors.txt b/tests/baselines/reference/predicateSemantics.errors.txt index 1c9de704f1628..0dc25d43dbf2b 100644 --- a/tests/baselines/reference/predicateSemantics.errors.txt +++ b/tests/baselines/reference/predicateSemantics.errors.txt @@ -96,4 +96,11 @@ predicateSemantics.ts(52,14): error TS2869: Right operand of ?? is unreachable b ~~~~~~~~~~ !!! error TS2869: Right operand of ?? is unreachable because the left operand is never nullish. } + + // https://github.com/microsoft/TypeScript/issues/60439 + class X { + constructor() { + const p = new.target ?? 32; + } + } \ No newline at end of file diff --git a/tests/baselines/reference/predicateSemantics.js b/tests/baselines/reference/predicateSemantics.js index 3641305b71805..f4edc198ffecd 100644 --- a/tests/baselines/reference/predicateSemantics.js +++ b/tests/baselines/reference/predicateSemantics.js @@ -54,6 +54,13 @@ function foo(this: Object | undefined) { const e = (i++, i++) ?? true; // error const f = (maybe, i++) ?? true; // error } + +// https://github.com/microsoft/TypeScript/issues/60439 +class X { + constructor() { + const p = new.target ?? 32; + } +} //// [predicateSemantics.js] @@ -106,3 +113,12 @@ function foo() { var e = (_h = (i++, i++)) !== null && _h !== void 0 ? _h : true; // error var f = (_j = (maybe, i++)) !== null && _j !== void 0 ? _j : true; // error } +// https://github.com/microsoft/TypeScript/issues/60439 +var X = /** @class */ (function () { + function X() { + var _newTarget = this.constructor; + var _a; + var p = (_a = _newTarget) !== null && _a !== void 0 ? _a : 32; + } + return X; +}()); diff --git a/tests/baselines/reference/predicateSemantics.symbols b/tests/baselines/reference/predicateSemantics.symbols index c91201e3fcda2..e5c831515a0bc 100644 --- a/tests/baselines/reference/predicateSemantics.symbols +++ b/tests/baselines/reference/predicateSemantics.symbols @@ -104,3 +104,15 @@ function foo(this: Object | undefined) { >i : Symbol(i, Decl(predicateSemantics.ts, 48, 5)) } +// https://github.com/microsoft/TypeScript/issues/60439 +class X { +>X : Symbol(X, Decl(predicateSemantics.ts, 52, 1)) + + constructor() { + const p = new.target ?? 32; +>p : Symbol(p, Decl(predicateSemantics.ts, 57, 9)) +>new.target : Symbol(X, Decl(predicateSemantics.ts, 52, 1)) +>target : Symbol(X, Decl(predicateSemantics.ts, 52, 1)) + } +} + diff --git a/tests/baselines/reference/predicateSemantics.types b/tests/baselines/reference/predicateSemantics.types index ee88a95cd16ea..de0c9daf76eb0 100644 --- a/tests/baselines/reference/predicateSemantics.types +++ b/tests/baselines/reference/predicateSemantics.types @@ -308,3 +308,23 @@ function foo(this: Object | undefined) { > : ^^^^ } +// https://github.com/microsoft/TypeScript/issues/60439 +class X { +>X : X +> : ^ + + constructor() { + const p = new.target ?? 32; +>p : 32 | typeof X +> : ^^^^^^^^^^^^^ +>new.target ?? 32 : 32 | typeof X +> : ^^^^^^^^^^^^^ +>new.target : typeof X +> : ^^^^^^^^ +>target : typeof X +> : ^^^^^^^^ +>32 : 32 +> : ^^ + } +} + diff --git a/tests/cases/compiler/predicateSemantics.ts b/tests/cases/compiler/predicateSemantics.ts index 88374b9a2ff6b..e937b1d6182a8 100644 --- a/tests/cases/compiler/predicateSemantics.ts +++ b/tests/cases/compiler/predicateSemantics.ts @@ -51,3 +51,10 @@ function foo(this: Object | undefined) { const e = (i++, i++) ?? true; // error const f = (maybe, i++) ?? true; // error } + +// https://github.com/microsoft/TypeScript/issues/60439 +class X { + constructor() { + const p = new.target ?? 32; + } +}