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

Exclude is bottlenecked by depth limit #53583

Closed
ufukbakan opened this issue Mar 30, 2023 · 6 comments
Closed

Exclude is bottlenecked by depth limit #53583

ufukbakan opened this issue Mar 30, 2023 · 6 comments
Labels
Duplicate An existing issue was already created

Comments

@ufukbakan
Copy link

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
}
@MartinJohns
Copy link
Contributor

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
Copy link

Also I’m not really sure what this has to do with a depth limit?

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 30, 2023
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@ufukbakan
Copy link
Author

Also I’m not really sure what this has to do with a depth limit?

I was aware its working with union types and thought when there is unlimited types , its bottlenecked.

@ufukbakan
Copy link
Author

ufukbakan commented Apr 4, 2023

However i still couldnt map my type, Omit is not working too :

type comments = {
    [key: string]: string
}

type hasSum = {
    sum: number
}

type mergedType = Omit<comments, "sum"> & hasSum;

const merged: mergedType = {
    sum: 1 // type error can not assign number to string
}

@fatcerberus
Copy link

fatcerberus commented Apr 4, 2023

I was aware its working with union types and thought when there is unlimited types , its bottlenecked.

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 like string & NOT "foo", so Exclude/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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants