我在理解与类型的循环引用有关的某些打字行为时遇到了困难.
type NonEmptyObject<T> = keyof T extends never ? never : T
type Base = { [key: PropertyKey]: Nested }; // I'm using this instead of Record to avoid a circular reference.
type NonEmptyBase = NonEmptyObject<Base>;
type Nested = string | NonEmptyBase;
Type alias 'NonEmptyBase' circularly references itself.ts(2456)
个
Type alias 'Nested' circularly references itself.ts(2456)
个
如果我切换到泛型,一切正常.但这看起来不对,感觉更像是一个循环.
type Base<T extends Base<T>> = { [key: PropertyKey]: Nested<T> };
type NonEmptyBase<T extends Base<T>> = NonEmptyObject<T>;
type Nested<T extends Base<T>> = string | NonEmptyBase<T>;
为什么会这样?如果没有泛型,我如何才能达到预期的结果?