microsoft / microsoft/TypeScript
Certain homomorphic mappings break assignability with exactOptionalPropertyTypes
Open
Nobody has claimed this yet.
Domain: flag: exactOptionalPropertyTypes
Help Wanted
Possible Improvement
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
🔎 Search Terms
eopt exactOptionalPropertyTypes homomorphic Omit unassignable
🕗 Version & Regression Information
- This is the behavior in every version I tried
⏯ Playground Link
💻 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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the reported behavior in the linked TypeScript playground with exactOptionalPropertyTypes enabled, using the WrappedT, Omit, and HomomorphicOmit examples. Investigate how homomorphic mapped types and conditional inference affect assignability, then verify that the mapped results remain assignable to T without the reported true | undefined error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100