Skip to content

fix: allow aliases with explicit func type annotations#135

Open
nickita-khylkouski wants to merge 2 commits intobloomberg:mainfrom
nickita-khylkouski:fix/issue-91-aliases
Open

fix: allow aliases with explicit func type annotations#135
nickita-khylkouski wants to merge 2 commits intobloomberg:mainfrom
nickita-khylkouski:fix/issue-91-aliases

Conversation

@nickita-khylkouski
Copy link
Copy Markdown

Summary

Fixes #91 - Type error when adding aliases with explicit func type annotations.

Problem

When using buildCommand with an explicit type annotation on the func parameter:

buildCommand({
  func: ({ repo }: { repo: string }) => {},
  parameters: {
    flags: { repo: { kind: 'parsed', parse: String } },
    aliases: { r: 'repo' },  // ❌ Error: 'string' not assignable to 'never'
  }
});

Root Cause

When func has an explicit type annotation, TypeScript's NoInfer wrapper prevents type inference from flowing to parameters. This causes keyof FLAGS to become never, which makes Aliases<never> - a type that only accepts undefined.

Solution

Modified TypedCommandFlagParameters to use Aliases<string> when keyof FLAGS is never:

  • When FLAGS can be inferred → strict type checking (Aliases<keyof FLAGS>)
  • When FLAGS cannot be inferred → fallback to Aliases<string> with runtime validation

Changes

  • packages/core/src/parameter/types.ts - Modified TypedCommandFlagParameters conditional type
  • packages/core/tests/type-inference.spec.ts - Added 4 type-level tests (positive, negative, edge case)
  • packages/core/tests/alias-runtime.spec.ts - Added 30 runtime tests for alias validation

Trade-off

This is a necessary trade-off due to TypeScript's type inference limitations:

  • Compile-time: Invalid alias targets are allowed when keyof FLAGS is never
  • Runtime: Invalid alias targets are caught by existing validation in resolveAliases
  • Benefit: Aliases now work with explicit function types (a valid and common pattern)

Test Results

  • ✅ All 1310 tests pass
  • ✅ 100% code coverage maintained
  • ✅ 34 new tests added (4 type-level, 30 runtime)

@nickita-khylkouski nickita-khylkouski requested a review from a team as a code owner January 23, 2026 00:58
Fixes bloomberg#91

When using buildCommand with an explicit type annotation on the
func parameter, the type system could not infer FLAGS from the
parameters, causing keyof FLAGS to be never. This made aliases
impossible to use since Aliases<never> only accepts undefined.

The fix changes TypedCommandFlagParameters to use Aliases<string>
when keyof FLAGS is never, allowing any string as an alias target.
Invalid alias targets are still caught at runtime by the existing
validation in resolveAliases.

This is a necessary trade-off due to TypeScript's type inference
limitations when explicit type annotations are used on function
parameters.

Signed-off-by: Nickita Khylkouski <90287684+nickita-khylkouski@users.noreply.github.com>
@molisani molisani added the core ⚙ Relates to the @stricli/core package label Jan 23, 2026
Signed-off-by: Nickita Khylkouski <90287684+nickita-khylkouski@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core ⚙ Relates to the @stricli/core package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Type error when adding aliases

2 participants