forked from babel/babel
/
createTypeAnnotationBasedOnTypeof.ts
52 lines (51 loc) 路 1.39 KB
/
createTypeAnnotationBasedOnTypeof.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import {
anyTypeAnnotation,
stringTypeAnnotation,
numberTypeAnnotation,
voidTypeAnnotation,
booleanTypeAnnotation,
genericTypeAnnotation,
identifier,
} from "../generated";
import type * as t from "../..";
/**
* Create a type annotation based on typeof expression.
*/
export default function createTypeAnnotationBasedOnTypeof(
type:
| "string"
| "number"
| "undefined"
| "boolean"
| "function"
| "object"
| "symbol",
):
| t.StringTypeAnnotation
| t.VoidTypeAnnotation
| t.NumberTypeAnnotation
| t.BooleanTypeAnnotation
| t.GenericTypeAnnotation
| t.AnyTypeAnnotation {
if (type === "string") {
return stringTypeAnnotation();
} else if (type === "number") {
return numberTypeAnnotation();
} else if (type === "undefined") {
return voidTypeAnnotation();
} else if (type === "boolean") {
return booleanTypeAnnotation();
} else if (type === "function") {
return genericTypeAnnotation(identifier("Function"));
} else if (type === "object") {
return genericTypeAnnotation(identifier("Object"));
} else if (type === "symbol") {
return genericTypeAnnotation(identifier("Symbol"));
} else if (type === "bigint") {
// todo: use BigInt annotation when Flow supports BigInt
// https://github.com/facebook/flow/issues/6639
return anyTypeAnnotation();
} else {
throw new Error("Invalid typeof value: " + type);
}
}