forked from babel/babel
/
createTypeAnnotationBasedOnTypeof.ts
48 lines (46 loc) 路 1.44 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
import {
anyTypeAnnotation,
stringTypeAnnotation,
numberTypeAnnotation,
voidTypeAnnotation,
booleanTypeAnnotation,
genericTypeAnnotation,
identifier,
} from "../generated";
import type * as t from "../..";
export default createTypeAnnotationBasedOnTypeof as {
(type: "string"): t.StringTypeAnnotation;
(type: "number"): t.NumberTypeAnnotation;
(type: "undefined"): t.VoidTypeAnnotation;
(type: "boolean"): t.BooleanTypeAnnotation;
(type: "function"): t.GenericTypeAnnotation;
(type: "object"): t.GenericTypeAnnotation;
(type: "symbol"): t.GenericTypeAnnotation;
(type: "bigint"): t.AnyTypeAnnotation;
};
/**
* Create a type annotation based on typeof expression.
*/
function createTypeAnnotationBasedOnTypeof(type: string): t.FlowType {
switch (type) {
case "string":
return stringTypeAnnotation();
case "number":
return numberTypeAnnotation();
case "undefined":
return voidTypeAnnotation();
case "boolean":
return booleanTypeAnnotation();
case "function":
return genericTypeAnnotation(identifier("Function"));
case "object":
return genericTypeAnnotation(identifier("Object"));
case "symbol":
return genericTypeAnnotation(identifier("Symbol"));
case "bigint":
// todo: use BigInt annotation when Flow supports BigInt
// https://github.com/facebook/flow/issues/6639
return anyTypeAnnotation();
}
throw new Error("Invalid typeof value: " + type);
}