-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
could-be-type.ts
37 lines (32 loc) · 948 Bytes
/
could-be-type.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* @license Use of this source code is governed by an MIT-style license that
* can be found in the LICENSE file at https://github.com/cartant/tsutils-etc
*/
import * as ts from "typescript";
import { isIntersectionType } from "./is-intersection-type";
import { isReferenceType } from "./is-reference-type";
import { isType } from "./is-type";
import { isUnionType } from "./is-union-type";
export function couldBeType(
type: ts.Type,
name: string | RegExp,
qualified?: {
name: RegExp;
typeChecker: ts.TypeChecker;
}
): boolean {
if (isReferenceType(type)) {
type = type.target;
}
if (isType(type, name, qualified)) {
return true;
}
if (isIntersectionType(type) || isUnionType(type)) {
return type.types.some(t => couldBeType(t, name, qualified));
}
const baseTypes = type.getBaseTypes();
if (!baseTypes) {
return false;
}
return baseTypes.some(t => couldBeType(t, name, qualified));
}