Skip to content

Commit 2ee782c

Browse files
committed
Correctly remove type variables from a union type
1 parent 26e58f9 commit 2ee782c

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/ir/typescript_types.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -489,9 +489,14 @@ def to_type_variable_free(self, factory):
489489
# type variable free.
490490
new_types = []
491491
for t in self.types:
492-
new_types.append(t.to_type_variable_free(factory)
493-
if t.is_combound()
494-
else t)
492+
if t.is_combound():
493+
new_type = t.to_type_variable_free(factory)
494+
elif t.is_type_var():
495+
bound = t.get_bound_rec(factory)
496+
new_type = factory.get_any_type() if bound is None else bound
497+
else:
498+
new_type = t
499+
new_types.append(new_type)
495500
return UnionType(new_types)
496501

497502
def unify_types(self, t1, factory, same_type=True):
@@ -787,7 +792,7 @@ def __init__(self, name="Array"):
787792
"T", variance=tp.Covariant)])
788793

789794

790-
class FunctionType(tp.TypeConstructor, ObjectType):
795+
class FunctionType(tp.TypeConstructor):
791796
def __init__(self, nr_type_parameters: int):
792797
name = "Function" + str(nr_type_parameters)
793798

@@ -811,6 +816,7 @@ def __init__(self, nr_type_parameters: int):
811816
features of typescript.
812817
"""
813818

819+
814820
def gen_type_alias_decl(gen,
815821
etype=None) -> ts_ast.TypeAliasDeclaration:
816822
""" Generate a Type Declaration (Type Alias)
@@ -836,6 +842,7 @@ def gen_type_alias_decl(gen,
836842
gen._add_node_to_parent(gen.namespace, type_alias_decl)
837843
return type_alias_decl
838844

845+
839846
def add_type_alias(gen, namespace, type_name, ta_decl):
840847
gen.context._add_entity(namespace, 'types', type_name, ta_decl.get_type())
841848
gen.context._add_entity(namespace, 'decls', type_name, ta_decl)

tests/test_typescript.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,17 @@ def test_union_type_unification2():
205205
assert (len(res) == 2 and
206206
res[t2.types[1]] == t1.types[4] and
207207
res[t2.types[2]] == t1.types[2])
208+
209+
210+
def test_union_to_type_variable_free():
211+
type_param = tp.TypeParameter("S")
212+
union = tst.UnionType([tst.NumberType(), type_param])
213+
214+
new_union = union.to_type_variable_free(tst.TypeScriptBuiltinFactory())
215+
assert new_union == tst.UnionType([tst.NumberType(),
216+
tst.TypeScriptBuiltinFactory().get_any_type()])
217+
218+
type_param = tp.TypeParameter("S", bound=tst.StringType())
219+
union = tst.UnionType([tst.NumberType(), type_param])
220+
new_union = union.to_type_variable_free(tst.TypeScriptBuiltinFactory())
221+
assert new_union == tst.UnionType([tst.NumberType(), tst.StringType()])

0 commit comments

Comments
 (0)