Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions src/generator/typescript.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,27 @@ auto TypeScript::operator()(const IREnumeration &entry) const -> void {

auto TypeScript::operator()(const IRObject &entry) const -> void {
const auto type_name{sourcemeta::core::mangle(entry.pointer, this->prefix)};
const auto has_additional{entry.additional.has_value()};
const auto has_typed_additional{
std::holds_alternative<IRType>(entry.additional)};
const auto allows_any_additional{
std::holds_alternative<bool>(entry.additional) &&
std::get<bool>(entry.additional)};

if (has_additional && entry.members.empty()) {
if (has_typed_additional && entry.members.empty()) {
this->output << "export type " << type_name << " = Record<string, "
<< sourcemeta::core::mangle(entry.additional->pointer,
this->prefix)
<< sourcemeta::core::mangle(
std::get<IRType>(entry.additional).pointer,
this->prefix)
<< ">;\n";
return;
}

if (allows_any_additional && entry.members.empty()) {
this->output << "export type " << type_name
<< " = Record<string, unknown>;\n";
return;
}

this->output << "export interface " << type_name << " {\n";

// We always quote property names for safety. JSON Schema allows any string
Expand All @@ -110,20 +121,29 @@ auto TypeScript::operator()(const IRObject &entry) const -> void {
<< ";\n";
}

if (has_additional) {
if (allows_any_additional) {
this->output << " [key: string]: unknown | undefined;\n";
} else if (has_typed_additional) {
// TypeScript index signatures must be a supertype of all property value
// types. We use a union of all member types plus the additional properties
// type plus undefined (for optional properties).
this->output << " [key: string]:\n";
this->output << " // As a notable limitation, TypeScript requires index "
"signatures\n";
this->output << " // to also include the types of all of its "
"properties, so we must\n";
this->output << " // match a superset of what JSON Schema allows\n";
for (const auto &[member_name, member_value] : entry.members) {
this->output << " "
<< sourcemeta::core::mangle(member_value.pointer,
this->prefix)
<< " |\n";
}

this->output << " "
<< sourcemeta::core::mangle(entry.additional->pointer,
this->prefix)
<< sourcemeta::core::mangle(
std::get<IRType>(entry.additional).pointer,
this->prefix)
<< " |\n";
this->output << " undefined;\n";
}
Expand Down
2 changes: 1 addition & 1 deletion src/ir/include/sourcemeta/codegen/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ struct IRObjectValue : IRType {
struct IRObject : IRType {
// To preserve the user's ordering
std::vector<std::pair<sourcemeta::core::JSON::String, IRObjectValue>> members;
std::optional<IRType> additional;
std::variant<bool, IRType> additional;
};

/// @ingroup ir
Expand Down
8 changes: 4 additions & 4 deletions src/ir/ir_default_compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ auto handle_object(const sourcemeta::core::JSON &schema,
members.emplace_back(entry.first, std::move(member_value));
}

std::optional<IRType> additional{std::nullopt};
std::variant<bool, IRType> additional{true};
if (subschema.defines("additionalProperties")) {
const auto &additional_schema{subschema.at("additionalProperties")};
const auto is_false{additional_schema.is_boolean() &&
!additional_schema.to_boolean()};
if (!is_false) {
if (additional_schema.is_boolean()) {
additional = additional_schema.to_boolean();
} else {
auto additional_pointer{sourcemeta::core::to_pointer(location.pointer)};
additional_pointer.push_back("additionalProperties");
additional = IRType{.pointer = std::move(additional_pointer)};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ export type FlexibleRecord_AdditionalProperties_AnyOf_ZIndex4 = string;

export type FlexibleRecord_AdditionalProperties_AnyOf_ZIndex3 = unknown[];

export interface FlexibleRecord_AdditionalProperties_AnyOf_ZIndex2 {
}
export type FlexibleRecord_AdditionalProperties_AnyOf_ZIndex2 = Record<string, unknown>;

export type FlexibleRecord_AdditionalProperties_AnyOf_ZIndex1 = boolean;

Expand All @@ -27,6 +26,9 @@ export interface FlexibleRecord {
"name": FlexibleRecord_Properties_Name;
"count"?: FlexibleRecord_Properties_Count;
[key: string]:
// As a notable limitation, TypeScript requires index signatures
// to also include the types of all of its properties, so we must
// match a superset of what JSON Schema allows
FlexibleRecord_Properties_Name |
FlexibleRecord_Properties_Count |
FlexibleRecord_AdditionalProperties |
Expand Down
Loading