Skip to content

The signature of certain functions do not match the signature of the implementation #55010

Description

@NilsIrl

Bug Report

Functions such as isArrayType() and isTupleType have a different signature in src/compiler/types.ts and in the implementation in src/checker/types.ts.

In src/compiler/types.ts:

isArrayType(type: Type): boolean;
isTupleType(type: Type): boolean;

In src/compiler/checker.ts:

isTupleType(type: Type): type is TupleTypeReference;
isArrayType(type: Type): type is TypeReference;

This means that the post-condition of these functions cannot be made use of in external code.

🔎 Search Terms

  • isArrayType, isTupleType
  • Signature, TypeChecker

🕗 Version & Regression Information

This was only an internal issue until these functions were exposed in #52467

💻 Code

if (typeChecker.isArrayType(type)) {
    // complains that type is not TypeReference
    typeChecker.getTypeArguments(type);
}

🙁 Actual behavior

TS2345 [ERROR]: Argument of type 'Type' is not assignable to parameter of type 'TypeReference'.
  Type 'Type' is missing the following properties from type 'TypeReference': target, objectFlags
                const typeArgument = checker.getTypeArguments(type);

🙂 Expected behavior

The code example given compiles without issues.

PS: I have a PR ready to be submitted to fix it for these particular functions

Activity

  1. RyanCavanaugh commented on Jul 13, 2023

    @RyanCavanaugh
    Member

    The internal signature is wrong, dangerous, and convenient, because they make negative implications that aren't correct (a thing that is not an ArrayType is not necessarily not a TypeReference). The external signature is correct, safe, and less-convenient.

    We think this is a good trade-off for us, but not for people who aren't as familiar with TS as people who are working on TS itself.

  2. NilsIrl commented on Jul 14, 2023

    @NilsIrl
    ContributorAuthor

    Could adding an ArrayType which extends TypeReference fix the problem?

  3. fatcerberus commented on Jul 14, 2023

    @fatcerberus

    Nils Andre (@NilsIrl) That won’t fix anything. The issue is that, if you say the return type is type is TypeReference and the function returns false, TS then assumes that type is not a TypeReference and narrows accordingly. Which is wrong and a potential footgun because it might still be a TypeReference even if it’s not an ArrayType.

    This is the same reason functions like Number.isFinite() don’t return x is number. See #15048.

  4. NilsIrl commented on Jul 14, 2023

    @NilsIrl
    ContributorAuthor

    The idea would then be to then the signature to this:

    interface ArrayType extends TypeReference {}
    
    isArrayType(type: Type): type is ArrayType;

    Which means that if the function then returns false, it doesn't mean type is not a TypeReference

  5. typescript-bot commented on Jul 17, 2023

    @typescript-bot
    Contributor

    This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

  6. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions