Skip to content

fix: resolve nested instantiations of the same generic alias - #58

Open
mmnavarr wants to merge 1 commit into
dmmulroy:mainfrom
mmnavarr:fix/nested-generic-alias-resolution
Open

mmnavarr wants to merge 1 commit into
dmmulroy:mainfrom
mmnavarr:fix/nested-generic-alias-resolution

Conversation

@mmnavarr

Copy link
Copy Markdown

Problem

Alias resolution treats finite nested instantiations of the same generic alias as recursion, so unknown and object hidden behind them go unreported:

type Maybe<T> = T | undefined;
type Hidden = Maybe<Maybe<unknown>>; // not reported by no-unknown-type-aliases
function load(): Maybe<Maybe<unknown>> {} // not reported by no-unknown-returns
function save(value: Maybe<Maybe<object>>) {} // not reported by no-object-parameters

Type arguments were evaluated with the alias-expansion set active at the point where the parameter was used, not where the argument was written. When the outer Maybe body reaches T, the inner Maybe<unknown> argument was resolved with Maybe already marked as expanding, so resolution stopped.

Fix

Each substitution now stores the alias-expansion set from where its argument was written: the reference site for explicit arguments, and inside the alias for defaults. Evaluation picks up from that set. Genuinely recursive aliases still terminate, because an alias referenced inside its own body is still being expanded at that point.

Tests

New no-unknown-type-aliases RuleTester cases:

  • Invalid: Maybe<Maybe<unknown>>, Identity<Identity<Identity<unknown>>>, Pair<string, Pair<number, unknown>>. The first fails before this change.
  • Valid (terminate without a finding): Loop<T> = Loop<T>, Nested<T> = T | Nested<Nested<T>>, Tree<T> with recursive children, Maybe<Maybe<string>>.

pnpm check passes, and the skill assets are re-synced.

Substituted type arguments were evaluated with the alias-expansion set of the reference site instead of where the argument was written, so finite nested instantiations such as Maybe<Maybe<unknown>> were treated as recursion and missed by no-unknown-type-aliases, no-unknown-returns, and no-object-parameters.
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

Successfully merging this pull request may close these issues.

1 participant