Skip to content

Commit 3bff526

Browse files
authored
Merge pull request #316 from ocsigen/fix-315
Fix 315
2 parents 8504c8c + fc9a19a commit 3bff526

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

.github/workflows/publish.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ jobs:
1414
dotnet:
1515
- 6.0.x
1616
node-version:
17-
- 16.x
17+
- 20.x
1818
ocaml-compiler:
19-
- 4.13.x
19+
- 5.0.x
2020

2121
runs-on: ${{ matrix.os }}
2222

@@ -25,7 +25,7 @@ jobs:
2525
uses: actions/checkout@v3
2626

2727
- name: Use .NET ${{ matrix.dotnet }}
28-
uses: actions/setup-dotnet@v2
28+
uses: actions/setup-dotnet@v3
2929
with:
3030
dotnet-version: ${{ matrix.dotnet }}
3131

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
- Fix a bug which generated unnecessarily duplicated option type (#315).
89

910
## [1.4.5] - 2023-07-13
1011
- Fix a bug which caused optional properties not to be recognized as optional (#312).

lib/Typer.fs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -539,20 +539,35 @@ module Type =
539539
boundTyprms |> List.map (fun x -> TypeVar x.name),
540540
MultipleLocation (funcs |> List.map (fun f -> f.loc))
541541
)
542+
let normalizeUnion (u: UnionType) : UnionType =
543+
let rec go ts =
544+
ts |> List.collect (function
545+
| Union u -> go u.types
546+
| t -> [t]
547+
)
548+
{ u with types = go u.types |> List.distinct }
549+
550+
let normalizeIntersection (i: IntersectionType) : IntersectionType =
551+
let rec go ts =
552+
ts |> List.collect (function
553+
| Intersection i -> go i.types
554+
| t -> [t]
555+
)
556+
{ i with types = go i.types |> List.distinct }
542557

543558
// TODO: more optimization
544-
let createUnion (_ctx: TyperContext<_, _>) (types: Type list) =
559+
let createUnion (types: Type list) =
545560
match types with
546561
| [] -> Prim Never
547562
| [x] -> x
548-
| _ -> Union { types = types }
563+
| _ -> Union (normalizeUnion { types = types })
549564

550565
// TODO: more optimization
551-
let createIntersection (_ctx: TyperContext<_, _>) (types: Type list) =
566+
let createIntersection (types: Type list) =
552567
match types with
553568
| [] -> Prim Any
554569
| [x] -> x
555-
| _ -> Intersection { types = types }
570+
| _ -> Intersection (normalizeIntersection { types = types })
556571

557572
let substTypeVarInInheritingType subst ctx = function
558573
| InheritingType.KnownIdent x ->
@@ -694,7 +709,7 @@ module Type =
694709
let members = c.members |> List.map snd
695710
let intersection = function
696711
| [] -> None
697-
| ts -> createIntersection ctx ts |> Some
712+
| ts -> createIntersection ts |> Some
698713
let rec go = function
699714
| TypeLiteral (LString name) ->
700715
let funcs, others =
@@ -815,7 +830,7 @@ module Type =
815830
| _ -> None)
816831
match types with
817832
| [] -> onFail ()
818-
| _ -> createIntersection ctx types
833+
| _ -> createIntersection types
819834

820835
| Keyof t ->
821836
let t = resolveErasedTypeImpl typeQueries ctx t

src/Targets/JsOfOCaml/Writer.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ let rec emitMembers flags overrideFunc ctx (selfTy: Type) (ma: MemberAttribute)
704704
if ma.isStatic then [Choice2Of2 (Prim Void)]
705705
else [Choice2Of2 PolymorphicThis]
706706
let ret =
707-
if fl.isOptional then Union { types = [fl.value; Prim Undefined] }
707+
if fl.isOptional then createUnion [fl.value; Prim Undefined]
708708
else fl.value
709709
func { isVariadic = false; args = args; returnType = ret; loc = ma.loc } |> emitType_ ctx
710710
yield! comments ()

0 commit comments

Comments
 (0)