ts-api-utils icon indicating copy to clipboard operation
ts-api-utils copied to clipboard

🐛 Bug: isTypeParameter should not be a type guard

Open danvk opened this issue 6 months ago • 2 comments

Bug Report Checklist

  • [X] I have tried restarting my IDE and the issue persists.
  • [X] I have pulled the latest main branch of the repository.
  • [X] I have searched for related issues and found none that matched my issue.

Expected

I'd expect code like this to produce the expected types:

function dealWithType(type: ts.Type) {
  if (tsutils.isTypeParameter(type)) {
    type  // type is ts.TypeParameter
  } else {
    type  // type is ts.Type… or something that includes all types other than type parameters
  }
}

Actual

The TS type of type in the else branch is never. This doesn't match reality at runtime, where the else branch is very much alive.

Additional Info

The issue is that this is how the types are declared in typescript.d.ts:

    interface InstantiableType extends Type {
    }
    interface TypeParameter extends InstantiableType {
    }

i.e. TypeParameter = Type.

So long as that's the case, it would be better if isTypeParameter were not declared as a type guard.

danvk avatar Feb 04 '24 21:02 danvk