Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pragmatic yet more correct narrowing algorithm for UDTP #47389

Closed
5 tasks done
devanshj opened this issue Jan 11, 2022 · 14 comments
Closed
5 tasks done

Pragmatic yet more correct narrowing algorithm for UDTP #47389

devanshj opened this issue Jan 11, 2022 · 14 comments
Labels
Experimentation Needed Someone needs to try this out to see what happens In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@devanshj
Copy link

devanshj commented Jan 11, 2022

Suggestion

πŸ” Search Terms

Narrowing, User defined typed predicate, first-filter narrowing

βœ… Viability Checklist

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code (Could break some code, but only the code that is already buggy and can produce runtime errors)
  • 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

Let's take an example...

interface Dog { woof: () => void }
interface Cat { meow: () => void }
interface Fish { swim: () => void }  

declare const isCatOrFish: (x: unknown) => x is Cat | Fish
declare let catOrDog: Cat | Dog
if (isCatOrFish(catOrDog)) {
  catOrDog.meow()
  catOrDog;
// ^? Cat
}

Okay so first off all, by definition of "narrowing", the narrowed catOrDog should have been (Cat | Dog) & (Cat | Fish) and not Cat, both are not same. But TypeScript makes a good pragmatic decision to make it Cat, and how does it do that? Via an algorithm Ryan describes here. To describe it better let me convert that algorithm into code...

type TypeScriptNarrow
  < T // to-narrow
  , N // narrowed
  , ShouldBePragmatic =
      // if any of the narrowed constituent is a subtype of any of the to-narrow constituent
      ( N extends unknown
          ? N extends T ? true : false
          : never
      ) extends false
        ? false
        : true
  > = 
    ShouldBePragmatic extends true
      ? T extends unknown
          ? Extract<T, N> 
          : never
      : T & N

interface Dog { woof: () => void }
interface Cat { meow: () => void }
interface Fish { swim: () => void }  
interface Monkey { climb: () => void } 

type T0 = TypeScriptNarrow<Dog | Cat, Cat>
//   ^? Cat
type T1 = TypeScriptNarrow<Dog | Cat, Cat | Fish>
//   ^? Cat
type T2 = TypeScriptNarrow<Dog | Monkey, Cat | Fish>
//   ^? (Dog | Monkey) & (Cat | Fish)
type T3 = TypeScriptNarrow<{ a: string | undefined } | { b: string }, { a: string } | { b: string }>
//   ^? { b: string }

The first three cases are good, the last one is problematic. Consider this...

const foo = (x: { a: string | undefined } | { b: string }) => {
  if (areValuesDefined(x)) {
    console.log(x.b.toUpperCase())
  }
}

const areValuesDefined = <T>(x: T): x is { [K in keyof T]: Exclude<T[K], undefined> } =>
  Object.values(x).every(x => x !== undefined)

foo({ a: "hello" }) // TypeError: Cannot read properties of undefined (reading 'toUpperCase')

Here x gets narrowed to { b: string } (instead of something like { a: string } | { b: string }) because of the current algorithm and makes the type system unsound.

So instead of the current algorithm I propose this algorithm...

type DevanshNarrow
  < T // to-narrow
  , N // narrowed
  , ShouldBePragmatic =
      // if any of the narrowed constituent is a subtype of any of the to-narrow constituent
      ( N extends unknown
          ? N extends T ? true : false
          : never
      ) extends false
        ? false
        : true
  > = 
    ShouldBePragmatic extends true
      ? T extends unknown
          ? N extends unknown
              ? keyof T & keyof N extends never
                  ? never
                  : T & N
              : never
          : never
      : T & N

interface Dog { woof: () => void }
interface Cat { meow: () => void }
interface Fish { swim: () => void }  
interface Monkey { climb: () => void } 

type T0 = DevanshNarrow<Dog | Cat, Cat>
//   ^? Cat
type T1 = DevanshNarrow<Dog | Cat, Cat | Fish>
//   ^? Cat
type T2 = DevanshNarrow<Dog | Monkey, Cat | Fish>
//   ^? (Dog | Monkey) & (Cat | Fish)
type T3 = DevanshNarrow<{ a: string | undefined } | { b: string }, { a: string } | { b: string }>
//   ^? | ({ a: string | undefined } & { a: string })
//      | ({ b: string } & { b: string })

So the first three results remain the same, hence it's still pragmatic but now the last result is more correct with DevanshNarrow than TypeScriptNarrow. With this algorithm the unsound code above would not compile.

The idea is instead of an actual intersection, we do a pragmatic intersection that is if two types don't overlap their pragmatic intersection is never. And hence Dog <pragmaticIntersect> Cat becomes never

I wonder if there are cases where TypeScript's current algorithm yields better results than the proposed.

πŸ“ƒ Motivating Example

πŸ’» Use Cases

@fatcerberus
Copy link

fatcerberus commented Jan 11, 2022

I’m not really sure what you’re asking for, but Dog & Cat is not an empty type by TypeScript’s typing rules: it’s perfectly fine to have a chimera with both meow and woof methods, and such an object would be assignable to both Dog and Cat, and therefore (by the definition of intersection) a valid instance of Dog & Cat. The only way it would make sense for Dog & Cat to be never would be if they were exact types, which is its own issue - #12936

@MartinJohns
Copy link
Contributor

it’s perfectly fine to have a chimera with both meow and woof methods, and such an object would be assignable to both Dog and Cat

image

@fatcerberus
Copy link

I guess the thing I’m having a hard time understanding is this: TypeScript types are defined in terms of assignability. A | B is a value which is assignable to either A or B. A & B is a value which is assignable to both simultaneously. These are both easy to understand in the abstract. How would this proposed β€œpragmatic intersect” be defined in terms of assignability?

@devanshj
Copy link
Author

devanshj commented Jan 11, 2022

I’m not really sure what you’re asking for

I'm asking for narrowing x in the above example to { a: string } | { b: string } (or something equivalent to that) instead of { b: string }

Dog & Cat is not an empty type

When did I say it was? Ofc it's not an empty type. I specifically mentioned "pragmatic intersect"

How would this proposed β€œpragmatic intersect” be defined in terms of assignability?

Why would you want to define it in terms of assignability?

To make it clear: I'm not proposing a new type construct, "pragmatic intersect" is just an internal operation the compiler would perform while narrowing a type passed through a UDTP.

@RyanCavanaugh
Copy link
Member

With this algorithm the unsound code above would not compile.

Can you expand on this? It's not clear to me what the putatively unsound code snippet is.

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Jan 11, 2022
@devanshj
Copy link
Author

devanshj commented Jan 11, 2022

This is snippet I'm talking about...

const foo = (x: { a: string | undefined } | { b: string }) => {
  if (areValuesDefined(x)) {
    x;
//  ^? { b: string }
    console.log(x.b.toUpperCase())
  }
}

const areValuesDefined = <T>(x: T): x is { [K in keyof T]: Exclude<T[K], undefined> } =>
  Object.values(x).every(x => x !== undefined)

foo({ a: "hello" }) // TypeError: Cannot read properties of undefined (reading 'toUpperCase')

If the narrowed x, ie the x on line 3 was ({ a: string | undefined } & { a: string }) | ({ b: string } & { b: string }) then the snippet won't compile and will give the following error...

Property 'b' does not exist on type '({ a: string | undefined; } & { a: string; }) | ({ b: string; } & { b: string; })'.
  Property 'b' does not exist on type '{ a: string | undefined; } & { a: string; }'.

Is that what you're asking?

@fatcerberus
Copy link

fatcerberus commented Jan 11, 2022

Hmm, hovering over areValuesDefined(x) shows that it's instantiated as:

const areValuesDefined: <{
    a: string | undefined;
} | {
    b: string;
}>(x: {
    a: string | undefined;
} | {
    b: string;
}) => x is {
    a: string;
} | {
    b: string;
}

...which is correct and is the narrowing behavior you want, but x is actually narrowed to { b: string } inside the block. Quite odd.

TS Playground

Even more curious is that disabling strictNullChecks makes the error you want appear:

Property 'b' does not exist on type '{ a: string; } | { b: string; }'.
  Property 'b' does not exist on type '{ a: string; }'.(2339)

I'm going to go out on a limb here and say this is a bona fide bug.

@RyanCavanaugh
Copy link
Member

@fatcerberus This is the expected behavior per #47283 (comment)

@RyanCavanaugh RyanCavanaugh added Experimentation Needed Someone needs to try this out to see what happens In Discussion Not yet reached consensus and removed Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Jan 11, 2022
@fatcerberus
Copy link

  • See if the narrowed-to type is a subtype of at least one constituent of the union
  • If yes, narrow to those constituents(s) of the union

{ a: string } is a subtype of { a: string | undefined }, so it seems like it shouldn't be filtered out?

@fatcerberus
Copy link

fatcerberus commented Jan 12, 2022

I think I see the problem--the filtering is being done on the original union (essentially taking only the members the input and output types have in common), rather than the narrowed one.

Would it produce more sound results if the logic was tweaked like this:

  • Compute the type that's narrowed to
  • See if the narrowed-to type is a subtype of at least one constituent of the un-narrowed union
  • If yes, narrow to those constituents(s) of the narrowed union
  • Otherwise, intersect the narrowed-type type with each member of the union

i.e. still "filter-first", but now you're doing the filtering on the narrowed-to type.

@RyanCavanaugh
Copy link
Member

It seems possible there's a good tweak here. The only way we find breaks in this area is to just write the PR and run the user code through it, since these rules were largely derived experimentally and bad effects show up pretty quickly in the user code suite.

@devanshj
Copy link
Author

devanshj commented Jan 12, 2022

Looking at narrowTypeByTypePredicate and getNarrowedType, I kinda understand it but it's unlikely I'll be able to pull it off :P

Also I've improved the proposed algorithm a bit...

- keyof T & keyof N extends never
-   ? never
-   : T & N
+ T extends N
+   ? T
+   : keyof T & keyof N extends never
+       ? never
+       : T & N

This will yield even better results

type Test = DevanshNarrow<
  { a: string | undefined } | { b: string, foo: string },
  { a: string } | { b: string }
>
// Before: 
// | ({ a: string | undefined } & { a: string })
// | ({ b: string, foo: string } & { b: string })
//
// After:
// | ({ a: string | undefined } & { a: string })
// | { b: stirng, foo: string }

@devanshj
Copy link
Author

devanshj commented Oct 2, 2022

Looks like the narrowing is sound for this snippet in v4.8.4 (was unsound in v4.7.4, versions not exact). It used to narrow x to { b: string } and now it correctly narrows it to { a: string } | { b: string }.

const foo = (x: { a: string | undefined } | { b: string }) => {
  if (areValuesDefined(x)) {
    // @ts-expect-error
    console.log(x.b.toUpperCase())
  }
}

const areValuesDefined = <T>(x: T): x is { [K in keyof T]: Exclude<T[K], undefined> } =>
  Object.values(x as {}).every(x => x !== undefined)

I wonder what was the change, whether it was intentional, and if this feature-request is still applicable (as this was the only unsound snippet I had in my mind xD).

I think this can be closed, I'll open a new one if I come across another unsound suboptimal narrowing.

@devanshj devanshj closed this as completed Oct 2, 2022
@Andarist
Copy link
Contributor

Andarist commented Oct 7, 2022

@devanshj this was fixed by #49625

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Experimentation Needed Someone needs to try this out to see what happens In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants