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.

Narrowing for key type by `in` operator

See original GitHub issue

Suggestion

A k in v check should narrow type of k: K to K & keyof typeof v.

🔍 Search Terms

in operator narrowing

✅ Viability 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript’s Design Goals.

⭐ Suggestion

This code should compile due to narrowing on x:

const o = {foo: 1, bar: 2} as const;
const x: string = 'foo';
if (x in o) {
    const y: "foo" | "bar" = x; // OK
}

Currently it could be made with custom type guard

const in_ = <K extends string, O>(key: K, object: O): key is K & keyof O => key in object;
if (in_(x, o)) {
    const y: "foo" | "bar" = x; // OK
}

📃 Motivating Example

When validating a JSON with some kind of AST (or otherwise containing a tagged union) the only type we can have for a tag is a string.

const validateDisjointUnion = <O extends object>(
    nodeTypeValidators: { [K in keyof O]: (value: O[K]) => boolean },
    value: Json,
) => {
    if (typeof value !== 'object' || !('type' in value)) throw 42;
    const {type} = value;
    if (typeof type !== 'string' || !(type in nodeTypeValidators)) throw 42;
    // expression of type 'string' can't be used to index type '{ [K in keyof O]: (value: O[K]) => boolean; }'.
    return nodeTypeValidators[type](value);
}

💻 Use Cases

in_ solves the issue, but it involves a type guard. Type guards are “dangerous” in a sense that they can be used improperly (for example, !(key in object) would compile as well, but would lead to runtime error).

There is a related feature request to narrow type for second parameter of in operator. As far as I’m aware, there is no syntax to specify type of in_ that would guard on both parameters at the same time.

const guardBoth = <A, B>(a: A, b: B): (a is string) & (b is string) => { ... };

Edit. Not even

// A type predicate cannot reference a rest parameter.(1229)
const in_ = <K extends string, O, T>(...args: [K, O]): args is [K & keyof O, O & { [L in K]: unknown }] => args[0] in args[1];

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:63
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

19reactions
Khufu-Icommented, Apr 15, 2021

I was surprised that Typescript doesn’t do this already

6reactions
jcalzcommented, Nov 30, 2022

Note that such narrowing is technically unsafe (although the current narrowing in #10485 is also technically unsafe for the same reason):

function f(v: { a: string }, k: string) {
  if (k in v) {
    // k is now "a" by #43284, so
    v[k].toUpperCase();
  }
}
const v = { a: "hello", b: 123 };
f(v, "b"); // kaboom

This is maybe fine, but people should be aware of it.

Also, the suggestion in #21732 (edit: implemented by #50666) and this suggestion acting in concert could do weird things, since on the one hand we’d be extending v to add a new known property, and on the other hand we’d be narrowing k, somewhat inconsistently:

declare const k: "bar";
declare const v: { foo: 0, baz: 1 };
if (k in v) {
  // k is now never by 43284
  // v is now {foo: 0, baz: 1, bar: unknown} by 50666
}

Maybe there’s some reasonable heuristic here? Like, if k is something wide like string then we want to narrow k and not v, but when k is narrow like a string literal we should… do something else?

Playground link

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Narrowing - TypeScript
JavaScript has an operator for determining if an object has a property with a name: the in operator. TypeScript takes this into account...
Read more >
Typescript: type narrowing not working for `in` when key is ...
Issue #10530 talks about narrowing via property access... like a.b or a["b"] , which does cause a to be narrowed, vs a[key] ,...
Read more >
How to Narrow a Type with the in Operator - Learn TypeScript
The in operator can narrow a type from a union. The left part of the operand is a string or a string literal....
Read more >
narrowing types via type guards and assertion functions - 2ality
Inside the then-blocks starting in line A and line B, the static type of value changes, due to the checks we performed. We...
Read more >
Narrowing or Broadening Your Search : Home - LibGuides
This guide teaches you ways to narrow or broaden your search by using keywords, limiters, and boolean operators. Narrowing or Broadening Your ...
Read more >

github_iconTop Related Medium Post

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