ts-json-schema-generator
Version:
Generate JSON schema from your Typescript sources
25 lines (18 loc) • 602 B
text/typescript
import { ExpectationFailedError } from "../Error/Errors.js";
import type { RawType, RawTypeName } from "../Schema/RawType.js";
export function typeName(value: RawType): RawTypeName {
if (value === null) {
return "null";
}
const type = typeof value;
if (type === "string" || type === "number" || type === "boolean") {
return type;
}
if (Array.isArray(value)) {
return "array";
}
if (type === "object") {
return "object";
}
throw new ExpectationFailedError(`JavaScript type "typeof " can't be converted to JSON type name`);
}