[ts] precise return type on createTypeAnnotationBasedOnTypeof (#13844)

This commit is contained in:
Mickey Rose 2021-10-14 09:00:19 +02:00 committed by GitHub
parent 2403a4889d
commit 24aa1b44fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

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