Skip to content

Certain homomorphic mappings break assignability with exactOptionalPropertyTypesΒ #60233

Description

πŸ”Ž 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Domain: flag: exactOptionalPropertyTypesBugs specific to enabling this flagHelp WantedYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some cases

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions