Skip to content

Expose isArrayType, isTupleType, isArrayLikeType in TypeChecker #37711

Closed
@ayazhafiz

Description

@ayazhafiz

Search Terms

isArrayType, isTupleType, isArrayLikeType

Suggestion

Expose isArrayType, isTupleType, isArrayLikeType in the TypeChecker interfaace

Use Cases

Today, a consumer of the TypeScript compiler API must roll their own implementation of these functions (or call into a type checker's private API) to validate some type. It would be convenient if these functions were exposed publicly instead.

This is somewhat related to #9879 but is of a much smaller scope -- here we only wish to provide comparison methods for array-like and tuple types.

Examples

Current:

import * as ts from 'typescript';

function isArrayType(type: Type, checker: ts.TypeChecker): boolean {
  return (checker as any).isArrayType(type);
}

let ty: ts.Type = makeType();
if (isArrayType(ty)) {
  // do some array things
}

Suggested:

let ty: ts.Type = makeType();
let checker: ts.TypeChecker = getTypeChecker();
if (checker.isArrayType(ty)) {
  // do some array things
}

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Metadata

Metadata

Assignees

No one assigned

    Labels

    In DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions