Debian Codegraph @

packages/babel-types/src/builders/flow/createTypeAnnotationBasedOnTypeof.ts

raw
1 import {
2 anyTypeAnnotation,
3 stringTypeAnnotation,
4 numberTypeAnnotation,
5 voidTypeAnnotation,
6 booleanTypeAnnotation,
7 genericTypeAnnotation,
8 identifier,
9 } from "../generated";
10 import type * as t from "../..";
11
12 export default createTypeAnnotationBasedOnTypeof as {
13 (type: "string"): t.StringTypeAnnotation;
14 (type: "number"): t.NumberTypeAnnotation;
15 (type: "undefined"): t.VoidTypeAnnotation;
16 (type: "boolean"): t.BooleanTypeAnnotation;
17 (type: "function"): t.GenericTypeAnnotation;
18 (type: "object"): t.GenericTypeAnnotation;
19 (type: "symbol"): t.GenericTypeAnnotation;
20 (type: "bigint"): t.AnyTypeAnnotation;
21 };
22
23 /**
24 * Create a type annotation based on typeof expression.
25 */
26 function createTypeAnnotationBasedOnTypeof(type: string): t.FlowType {
27 switch (type) {
28 case "string":
29 return stringTypeAnnotation();
30 case "number":
31 return numberTypeAnnotation();
32 case "undefined":
33 return voidTypeAnnotation();
34 case "boolean":
35 return booleanTypeAnnotation();
36 case "function":
37 return genericTypeAnnotation(identifier("Function"));
38 case "object":
39 return genericTypeAnnotation(identifier("Object"));
40 case "symbol":
41 return genericTypeAnnotation(identifier("Symbol"));
42 case "bigint":
43 // todo: use BigInt annotation when Flow supports BigInt
44 // https://github.com/facebook/flow/issues/6639
45 return anyTypeAnnotation();
46 }
47 throw new Error("Invalid typeof value: " + type);
48 }