Skip to content

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

@JamesParrott

Description

@JamesParrott

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>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions