Skip to content

Commit df27ebe

Browse files
committed
feat: maybe as union config
- e.g. `Maybe<string>`` vs. `string | null``
1 parent b52185e commit df27ebe

File tree

4 files changed

+6
-3
lines changed

4 files changed

+6
-3
lines changed

lib/anchor/config.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ class Config
66
:use_active_record_comment,
77
:infer_nullable_relationships_as_optional,
88
:empty_relationship_type,
9-
:use_type_as_schema_name
9+
:use_type_as_schema_name,
10+
:maybe_as_union
1011

1112
def initialize
1213
@ar_column_to_type = nil
@@ -16,6 +17,7 @@ def initialize
1617
@infer_nullable_relationships_as_optional = nil
1718
@empty_relationship_type = nil
1819
@use_type_as_schema_name = nil
20+
@maybe_as_union = nil
1921
end
2022
end
2123
end

lib/anchor/type_script/file_structure.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ def references_from_type(type)
8585
end
8686

8787
def utils_to_import
88+
return [] if Anchor.config.maybe_as_union
8889
maybe_type = has_maybe?(@object).presence && Anchor::Types::Reference.new("Maybe")
8990
[maybe_type].compact
9091
end

lib/anchor/type_script/serializer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def type_string(type, depth = 1)
1111
when Anchor::Types::Null.singleton_class then "null"
1212
when Anchor::Types::Record, Anchor::Types::Record.singleton_class then "Record<string, #{type_string(type.try(:value_type) || Anchor::Types::Unknown)}>"
1313
when Anchor::Types::Union then type.types.map { |type| type_string(type, depth) }.join(" | ")
14-
when Anchor::Types::Maybe then "Maybe<#{type_string(type.type, depth)}>"
14+
when Anchor::Types::Maybe then Anchor.config.maybe_as_union ? type_string(Anchor::Types::Union.new([type.type, Anchor::Types::Null]), depth) : "Maybe<#{type_string(type.type, depth)}>"
1515
when Anchor::Types::Array then "Array<#{type_string(type.type, depth)}>"
1616
when Anchor::Types::Literal then serialize_literal(type.value)
1717
when Anchor::Types::Reference then type.name

spec/example/lib/tasks/anchor.rake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ namespace :anchor do
4949
exclude_fields: nil,
5050
manually_editable: true,
5151
)
52-
modified_files = Anchor::TypeScript::MultifileSaveService.call(generator:, folder_path:)
52+
modified_files = Anchor::TypeScript::MultifileSaveService.call(generator:, folder_path:, force: true)
5353
puts modified_files.join(" ")
5454
end
5555
end

0 commit comments

Comments
 (0)