Closed
Description
When excluding a type from an unlimited type , it should calculate excluded type first but it doesnt. For example:
type NonZ = Exclude<string, 'Z'>;
const myval: NonZ = 'Z'; // doesnt give any error
Because of this bug many other bugs happens like :
interface HasNotZ {
[key: NonZ]: number
}
const MyObj : HasNotZ = {
Z: 123 // is still valid .... Should give an error
}
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
MartinJohns commentedon Mar 30, 2023
You forgot to fill out the issue template.
This is working as intended.
Exclude<>
is used to remove types from a union type.string
is not a union type, so the literal type'Z'
can be removed from it. For this to work TypeScript would need to support types like "a string, but not...", which it does not. The relevant issues for this are #4196 / #29317.fatcerberus commentedon Mar 30, 2023
Also I’m not really sure what this has to do with a depth limit?
typescript-bot commentedon Apr 1, 2023
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.
ufukbakan commentedon Apr 4, 2023
I was aware its working with union types and thought when there is unlimited types , its bottlenecked.
ufukbakan commentedon Apr 4, 2023
However i still couldnt map my type, Omit is not working too :
fatcerberus commentedon Apr 4, 2023
No, the depth limit is about types that are defined recursively, it has nothing to do with the number of values inhabiting the type or even whether the type is finite. The reason this doesn't work is simply because primitives like
string
are not union types and TS has no way internally to represent types likestring & NOT "foo"
, soExclude
/Omit
can't do anything with them.This is effectively a duplicate of #4196 because you're what you're trying to do requires support for negated types.