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

Certain homomorphic mappings break assignability with exactOptionalPropertyTypes #60233

Open
ssalbdivad opened this issue Oct 15, 2024 · 0 comments Β· May be fixed by #60890
Open

Certain homomorphic mappings break assignability with exactOptionalPropertyTypes #60233

ssalbdivad opened this issue Oct 15, 2024 · 0 comments Β· May be fixed by #60890
Labels
Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone

Comments

@ssalbdivad
Copy link

ssalbdivad commented Oct 15, 2024

πŸ”Ž Search Terms

eopt exactOptionalPropertyTypes homomorphic Omit unassignable

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

https://tsplay.dev/w28Qzm

πŸ’» Code

// @exactOptionalPropertyTypes

type T = {
  foo?: true;
  bar?: true;
};

type WrappedT<t extends T> = [t];

// Since Omit is normally treated as homomorphic, should work but errors with:
// Type 'true | undefined' is not assignable to type 'true'.
type OmitBarFromWrapped<t> =
  t extends WrappedT<infer inner> ? WrappedT<Omit<inner, "bar">> : never;

// Even if we directly rewrite Omit to be homomorphic, we still lose assignability with EOPT
type OmitHomomorphicFromWrapped<t> =
  t extends WrappedT<infer inner>
    ? WrappedT<HomomorphicOmit<inner, "bar">>
    : never;

type HomomorphicOmit<t, keyToOmit> = {
  [k in keyof t as k extends keyToOmit ? never : k]: t[k];
};

// Normally in this situation, I have a conform utility to force TS to allow the type
// but this is maybe the first time I've also seen that not work?
type OmitHomomorphicFromWrappedConformed<t> =
  t extends WrappedT<infer inner>
    ? WrappedT<conform<HomomorphicOmit<inner, "bar">, T>>
    : never;

type conform<t, base> = t extends base ? t : base;

πŸ™ Actual behavior

Applying a homomorphic mapping to a type can break property assignability with EOPT

πŸ™‚ Expected behavior

Should preserve assignability

Additional information about the issue

No response

@RyanCavanaugh RyanCavanaugh added Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases Help Wanted You can do this labels Nov 13, 2024
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Nov 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Projects
None yet
2 participants