IfNotAnyOrNever: Lazily evaluate different conditional branches#1462
Merged
Conversation
som-sm
commented
Jun 20, 2026
| ``` | ||
| // When `T` is a NOT `any` or `never` (like `string`) => Returns `IfNotAnyOrNever` branch | ||
| type A = IfNotAnyOrNever<string, 'VALID', 'IS_ANY', 'IS_NEVER'>; | ||
| type A = IfNotAnyOrNever<string, {ifNot: 'VALID'; ifAny: 'IS_ANY'; ifNever: 'IS_NEVER'}>; |
Collaborator
Author
There was a problem hiding this comment.
Umm... would this be better?
Suggested change
| type A = IfNotAnyOrNever<string, {ifNot: 'VALID'; ifAny: 'IS_ANY'; ifNever: 'IS_NEVER'}>; | |
| type A = IfNotAnyOrNever<string, {neither: 'VALID'; any: 'IS_ANY'; never: 'IS_NEVER'}>; |
chatman-media
added a commit
to chatman-media/type-fest
that referenced
this pull request
Jun 22, 2026
Revert the `IfNotAnyOrNever` wrapper back to the inline `IsAny<TArray> extends true ? any : ...` form from the proposed implementation. The wrapper eagerly instantiates the recursive `SplitOnRestElement` body for `any`, producing "Type instantiation is excessively deep" (only resolved once sindresorhus#1462 changes `IfNotAnyOrNever` to lazily evaluate its branches). The inline short-circuit keeps the body in a non-instantiated branch, so `LastArrayElement<any>` resolves to `any` and the whole suite passes on `main` today without depending on sindresorhus#1462.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Consider the following example:
CrashIfAnyis a type that fails when instantiated withany. To fix this, we try wrapping it inIfNotAnyOrNeverto handle theanycase explicitly, but this currently doesn't prevent the error. This happens because when we writeIfNotAnyOrNever<T, CrashIfAny<T>, 'any handled'>,CrashIfAnyis instantiated withTeven if its value isn't going to be used.Couldn't really find a great fix for this, but accepting the values for different cases in an object seems to fix this issue. It seems to make the compiler not do the instantiation until the value is actually accessed.