question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Narrow typeof x === 'object' to Record<string | number | symbol, unknown> | null | unknown[]

See original GitHub issue

Search Terms

typeof object narrow record

Suggestion

When narrowing a variable with typeof x === 'object', narrow to Record<string | number | symbol, unknown> | null | unknown[] instead of object | null.

Use Cases

When testing to see if something is an object, you almost always are starting out with unknown or similarly near-top level type and trying to narrow down to a lower “useful” type. When you use the typeof x === 'object' narrowing operation, currently TypeScript over narrows to the extremely constrained {} | null type. This suggestion would change that behavior to narrow down to the much wider Record<string | number | symbol, unknown> | null | unknown[], which the user can then choose to narrow more if they like, or they can choose to work with it as-is.

Examples

declare const apple: unknown
if (typeof apple !== 'object') throw new Error() // actual: object | null; desired: Record<string|number|symbol, unknown> | null | unknown[]

// uncomment this line and comment out the lines above to see desired behavior
// declare const apple: Record<string|number|symbol, unknown> | null | unknown[]

if (apple === null) throw new Error() // actual: object; desired: Record<string|number|symbol, unknown> | unknown[]
if (Array.isArray(apple)) throw new Error() // actual: object; desired: Record<string|number|symbol, unknown>
for (const key in apple) {
    // Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.
    //   No index signature with a parameter of type 'string' was found on type '{}'.
    apple[key] // actual: error; desired: unknown
}

Checklist

My suggestion meets these guidelines:

  • This wouldn’t be a breaking change in existing TypeScript/JavaScript code
  • This wouldn’t change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript’s Design Goals.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:36
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
RyanCavanaughcommented, Jun 4, 2020

The great thing about users is that they write code you’d never expect.

Anyway one example would be

type IsArray<T> = T extends unknown[] ? true : false;

IsArray<typeof something_narrowed_to_object> is false today, the proposed change would make it true | false

2reactions
KotlinIslandcommented, Jan 6, 2022

@MicahZoltu You should update the title to: “Narrow typeof x === 'object' to Record<string | number | symbol, unknown> | null | unknown[]

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there a way to narrow an unknown type to Record<string ...
function isRecord(value: unknown): value is Record<string, unknown> { return typeof value === 'object' && value !== null; } function fun1(x: ...
Read more >
Narrowing an object of type unknown - Stack Overflow
Most solutions I find state to do something like this. const result: unknown = { movieName: "test", }; if ( typeof ...
Read more >
Documentation - Narrowing - TypeScript
TypeScript is warning us that adding a number | string to a number might not ... But it turns out that in JavaScript,...
Read more >
How To Do Anything in TypeScript With Type Guards
These are some common examples of narrowing: unknown or any to string; string | object | number to string; number | null |...
Read more >
Announcing TypeScript 4.9 - Microsoft Developer Blogs
Types can describe things like the shapes of our objects, ... while the type of packageJSON was narrowed from unknown to object ,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found