Skip to content

Commit 128237f

Browse files
fix: make isTypeParameter no longer a typeguard (#597)
Co-authored-by: Rebecca Stevens <rebecca.stevens@outlook.co.nz>
1 parent cbfd5dc commit 128237f

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

src/types/typeGuards/single.test.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import ts from "typescript";
2-
import { describe, expect, it } from "vitest";
2+
import { describe, expect, expectTypeOf, it } from "vitest";
33

44
import { createSourceFileAndTypeChecker } from "../../test/utils";
55
import {
66
isConditionalType,
77
isEnumType,
88
isIntersectionType,
99
isObjectType,
10+
isTypeParameter,
1011
isUnionOrIntersectionType,
1112
isUnionType,
1213
isUniqueESSymbolType,
@@ -74,6 +75,45 @@ describe("isObjectType", () => {
7475
});
7576
});
7677

78+
describe("isTypeParameter", () => {
79+
it("should return true for a type parameter", () => {
80+
const sourceText = `type Test<TParam> = { foo: string };`;
81+
const { sourceFile, typeChecker } =
82+
createSourceFileAndTypeChecker(sourceText);
83+
84+
const testNode = sourceFile.statements.at(-1) as ts.TypeAliasDeclaration;
85+
const tParamNode = testNode.typeParameters?.[0];
86+
expect(tParamNode).toBeDefined();
87+
88+
const tParamType = typeChecker.getTypeAtLocation(tParamNode!);
89+
90+
const isTParamATypeParameter = isTypeParameter(tParamType);
91+
// type test - see https://github.com/JoshuaKGoldberg/ts-api-utils/issues/382
92+
if (isTParamATypeParameter) {
93+
expectTypeOf(tParamType).toEqualTypeOf<ts.TypeParameter>();
94+
} else {
95+
expectTypeOf(tParamType).toEqualTypeOf<ts.Type>();
96+
}
97+
98+
expect(isTParamATypeParameter).toBe(true);
99+
});
100+
101+
it("should return false when not a type parameter", () => {
102+
const sourceText = `type Test<T> = { foo: string };`;
103+
const testType = getTypeForTypeNode(sourceText);
104+
105+
const isTestATypeParameter = isTypeParameter(testType);
106+
// type test - see https://github.com/JoshuaKGoldberg/ts-api-utils/issues/382
107+
if (isTestATypeParameter) {
108+
expectTypeOf(testType).toEqualTypeOf<ts.TypeParameter>();
109+
} else {
110+
expectTypeOf(testType).toEqualTypeOf<ts.Type>();
111+
}
112+
113+
expect(isTestATypeParameter).toBe(false);
114+
});
115+
});
116+
77117
describe("isUnionOrIntersectionType", () => {
78118
it.each([
79119
[false, "type Test = 1;"],

src/types/typeGuards/single.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ export function isSubstitutionType(type: ts.Type): type is ts.SubstitutionType {
168168

169169
/**
170170
* Test if a type is a `TypeParameter`.
171+
*
172+
* Note: It is intentional that this is not a type guard.
173+
* @see https://github.com/JoshuaKGoldberg/ts-api-utils/issues/382
171174
* @category Types - Type Guards
172175
* @example
173176
* ```ts
@@ -178,7 +181,7 @@ export function isSubstitutionType(type: ts.Type): type is ts.SubstitutionType {
178181
* }
179182
* ```
180183
*/
181-
export function isTypeParameter(type: ts.Type): type is ts.TypeParameter {
184+
export function isTypeParameter(type: ts.Type): boolean {
182185
return isTypeFlagSet(type, ts.TypeFlags.TypeParameter);
183186
}
184187

0 commit comments

Comments
 (0)