Skip to content
Open
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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,7 @@
"volta": {
"node": "20.1.0",
"npm": "8.19.4"
},
"dependencies": {
}
}
8 changes: 8 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25682,6 +25682,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}

function getDefinitelyFalsyPartOfType(type: Type): Type {
if (type.flags & TypeFlags.TypeParameter) {
const constraint = getBaseConstraintOfType(type);
if (!constraint || constraint === type) {
return neverType;
}
return getDefinitelyFalsyPartOfType(constraint);
}

return type.flags & TypeFlags.String ? emptyStringType :
type.flags & TypeFlags.Number ? zeroType :
type.flags & TypeFlags.BigInt ? zeroBigIntType :
Expand Down
4 changes: 2 additions & 2 deletions src/server/editorServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1003,8 +1003,8 @@ function callbackRefProject<T, P extends string>(
cb: (refProj: ConfiguredProject) => T | undefined,
refPath: P | undefined,
) {
const refProject = refPath && project.projectService.configuredProjects.get(refPath);
return refProject && cb(refProject);
const refProject = refPath ? project.projectService.configuredProjects.get(refPath) : undefined;
return refProject ? cb(refProject) : undefined;
}

function forEachReferencedProject<T>(
Expand Down
28 changes: 28 additions & 0 deletions tests/baselines/reference/andAndGeneric.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//// [tests/cases/compiler/andAndGeneric.ts] ////

//// [andAndGeneric.ts]
declare function id<T>(x: T): T;

function f<T>(x: T) {
return id(x && x); // should NOT be narrowed to NonNullable<T>
}

// ---- expected types ----
const t1 = f(null); // null (currently incorrectly NonNullable<null> -> never)
const t2 = f(0); // 0
const t3 = f(1); // 1
const t4 = f<"a" | null>("a"); // "a"
const t5 = f<"a" | null>(null); // null


//// [andAndGeneric.js]
"use strict";
function f(x) {
return id(x && x); // should NOT be narrowed to NonNullable<T>
}
// ---- expected types ----
var t1 = f(null); // null (currently incorrectly NonNullable<null> -> never)
var t2 = f(0); // 0
var t3 = f(1); // 1
var t4 = f("a"); // "a"
var t5 = f(null); // null
43 changes: 43 additions & 0 deletions tests/baselines/reference/andAndGeneric.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//// [tests/cases/compiler/andAndGeneric.ts] ////

=== andAndGeneric.ts ===
declare function id<T>(x: T): T;
>id : Symbol(id, Decl(andAndGeneric.ts, 0, 0))
>T : Symbol(T, Decl(andAndGeneric.ts, 0, 20))
>x : Symbol(x, Decl(andAndGeneric.ts, 0, 23))
>T : Symbol(T, Decl(andAndGeneric.ts, 0, 20))
>T : Symbol(T, Decl(andAndGeneric.ts, 0, 20))

function f<T>(x: T) {
>f : Symbol(f, Decl(andAndGeneric.ts, 0, 32))
>T : Symbol(T, Decl(andAndGeneric.ts, 2, 11))
>x : Symbol(x, Decl(andAndGeneric.ts, 2, 14))
>T : Symbol(T, Decl(andAndGeneric.ts, 2, 11))

return id(x && x); // should NOT be narrowed to NonNullable<T>
>id : Symbol(id, Decl(andAndGeneric.ts, 0, 0))
>x : Symbol(x, Decl(andAndGeneric.ts, 2, 14))
>x : Symbol(x, Decl(andAndGeneric.ts, 2, 14))
}

// ---- expected types ----
const t1 = f(null); // null (currently incorrectly NonNullable<null> -> never)
>t1 : Symbol(t1, Decl(andAndGeneric.ts, 7, 5))
>f : Symbol(f, Decl(andAndGeneric.ts, 0, 32))

const t2 = f(0); // 0
>t2 : Symbol(t2, Decl(andAndGeneric.ts, 8, 5))
>f : Symbol(f, Decl(andAndGeneric.ts, 0, 32))

const t3 = f(1); // 1
>t3 : Symbol(t3, Decl(andAndGeneric.ts, 9, 5))
>f : Symbol(f, Decl(andAndGeneric.ts, 0, 32))

const t4 = f<"a" | null>("a"); // "a"
>t4 : Symbol(t4, Decl(andAndGeneric.ts, 10, 5))
>f : Symbol(f, Decl(andAndGeneric.ts, 0, 32))

const t5 = f<"a" | null>(null); // null
>t5 : Symbol(t5, Decl(andAndGeneric.ts, 11, 5))
>f : Symbol(f, Decl(andAndGeneric.ts, 0, 32))

75 changes: 75 additions & 0 deletions tests/baselines/reference/andAndGeneric.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//// [tests/cases/compiler/andAndGeneric.ts] ////

=== andAndGeneric.ts ===
declare function id<T>(x: T): T;
>id : <T>(x: T) => T
> : ^ ^^ ^^ ^^^^^
>x : T
> : ^

function f<T>(x: T) {
>f : <T>(x: T) => NonNullable<T>
> : ^ ^^ ^^ ^^^^^^^^^^^^^^^^^^^
>x : T
> : ^

return id(x && x); // should NOT be narrowed to NonNullable<T>
>id(x && x) : NonNullable<T>
> : ^^^^^^^^^^^^^^
>id : <T_1>(x: T_1) => T_1
> : ^^^^^^ ^^ ^^^^^
>x && x : NonNullable<T>
> : ^^^^^^^^^^^^^^
>x : T
> : ^
>x : NonNullable<T>
> : ^^^^^^^^^^^^^^
}

// ---- expected types ----
const t1 = f(null); // null (currently incorrectly NonNullable<null> -> never)
>t1 : never
> : ^^^^^
>f(null) : never
> : ^^^^^
>f : <T>(x: T) => NonNullable<T>
> : ^ ^^ ^^ ^^^^^^^^^^^^^^^^^^^

const t2 = f(0); // 0
>t2 : 0
> : ^
>f(0) : 0
> : ^
>f : <T>(x: T) => NonNullable<T>
> : ^ ^^ ^^ ^^^^^^^^^^^^^^^^^^^
>0 : 0
> : ^

const t3 = f(1); // 1
>t3 : 1
> : ^
>f(1) : 1
> : ^
>f : <T>(x: T) => NonNullable<T>
> : ^ ^^ ^^ ^^^^^^^^^^^^^^^^^^^
>1 : 1
> : ^

const t4 = f<"a" | null>("a"); // "a"
>t4 : "a"
> : ^^^
>f<"a" | null>("a") : "a"
> : ^^^
>f : <T>(x: T) => NonNullable<T>
> : ^ ^^ ^^ ^^^^^^^^^^^^^^^^^^^
>"a" : "a"
> : ^^^

const t5 = f<"a" | null>(null); // null
>t5 : "a"
> : ^^^
>f<"a" | null>(null) : "a"
> : ^^^
>f : <T>(x: T) => NonNullable<T>
> : ^ ^^ ^^ ^^^^^^^^^^^^^^^^^^^

14 changes: 14 additions & 0 deletions tests/cases/compiler/andAndGeneric.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// @strict: true

declare function id<T>(x: T): T;

function f<T>(x: T) {
return id(x && x); // should NOT be narrowed to NonNullable<T>
}

// ---- expected types ----
const t1 = f(null); // null (currently incorrectly NonNullable<null> -> never)
const t2 = f(0); // 0
const t3 = f(1); // 1
const t4 = f<"a" | null>("a"); // "a"
const t5 = f<"a" | null>(null); // null
Loading