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

TSC warning/error using Range with numbers instead of str args, not numbers "Type instantiation is excessively deep and possibly infinite.", #347

Open
JamesParrott opened this issue Dec 24, 2024 · 0 comments

Comments

@JamesParrott
Copy link

JamesParrott commented Dec 24, 2024

Sorrry if this is due to a mistake of mine, or if it's a known issue. But quoting literal numbers makes little sense type-wise to me, and I easily found a work around on SO which typescript is happy with.

Bug:

import {N} from 'ts-toolbelt';
type T = N.Range<1, 100>;

Error/warning

[{
	"resource":...,
	"owner": "typescript",
	"code": "2589",
	"severity": 8,
	"message": "Type instantiation is excessively deep and possibly infinite.",
	"source": "ts",
	"startLineNumber": ...,
	"startColumn": ...,
	"endLineNumber": ....,
	"endColumn": ...
}]

Alternative implementation from Guillaume Mastio:

type Enumerate<N extends number, Acc extends number[] = []> = Acc['length'] extends N
  ? Acc[number]
  : Enumerate<N, [...Acc, Acc['length']]>

type Range<F extends number, T extends number> = Exclude<Enumerate<T>, Enumerate<F>>

type T = Range<20, 100>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant