-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issue
Milestone
Description
π Search Terms
narrow, generic
π Version & Regression Information
- This changed between versions 5.3.3 and 5.4.2
β― Playground Link
π» Code
// === WORKS =============
declare function isPlainObject1(
data: unknown,
): data is Record<PropertyKey, unknown>;
declare const myObj1: unknown;
if (isPlainObject1(myObj1)) {
for (const key of ["a", "b", "c"]) {
const deeper = myObj1[key];
// ^?
const deeperKeys = isPlainObject1(deeper) ? Object.keys(deeper) : [];
}
}
// === DOESNT WORK ================
// Only difference between the 2 impls is the generic T in the type, even when it isn't used...
declare function isPlainObject2<T>(
data: unknown,
): data is Record<PropertyKey, unknown>;
declare const myObj2: unknown;
if (isPlainObject2(myObj2)) {
for (const key of ["a", "b", "c"]) {
const deeper = myObj2[key];
// ^?
const deeperKeys = isPlainObject2(deeper) ? Object.keys(deeper) : [];
}
}π Actual behavior
The deeper variable, which is nested deeper from the narrowed type is inferred as any.
π Expected behavior
It is the result of accessing a prop on a Record<PropertyKey, unknown> so is expected to be unknown.
Additional information about the issue
I maintain the Remeda utility functions library, I just got this reported here: remeda/remeda#559 and there are additional anecdotal reports that upgrading to typescript 5.4 breaks several of our types.
I couldn't find a reasoning to this issue, including any new changes to Typescript in 5.4 which might explain it.
I'm not sure my title makes sense for this bug, as I don't fully understand what's going on here, or even how to build a truly minimal example.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issue