1
+ from src .ir .builtins import NumberType
1
2
import src .ir .typescript_types as tst
2
3
import src .ir .typescript_ast as ts_ast
3
4
import src .ir .types as tp
@@ -130,7 +131,7 @@ def test_union_type_unification_type_var():
130
131
assert type_var_map == {type_param : union }
131
132
132
133
133
- def test_unify_two_union_types ():
134
+ def test_union_type_unification ():
134
135
type_param = tp .TypeParameter ("T" )
135
136
union1 = tst .UnionType ([tst .NumberLiteralType (1410 ), tst .NumberType (), tst .StringType ()])
136
137
union2 = tst .UnionType ([type_param , tst .NumberType (), tst .StringType ()])
@@ -147,3 +148,35 @@ def test_unify_two_union_types():
147
148
assert len (type_var_map ) == 2
148
149
assert type_param , type_param2 in type_var_map
149
150
assert union1 .types [1 ], union1 .types [2 ] in type_var_map .values ()
151
+
152
+ def test_union_type_unification2 ():
153
+ union = tst .UnionType ([tst .NumberType (), tst .StringType ()])
154
+ assert tu .unify_types (tst .BooleanType (), union , tst .TypeScriptBuiltinFactory ()) == {}
155
+
156
+ t1 = tst .NumberType ()
157
+ t2 = tst .UnionType ([tst .NumberType (), tp .TypeParameter ("T" )])
158
+ res = tu .unify_types (t1 , t2 , tst .TypeScriptBuiltinFactory ())
159
+ assert len (res ) == 1 and res [t2 .types [1 ]] == t1
160
+
161
+ t1 = tst .UnionType ([tst .NumberType (), tst .StringType ()])
162
+ res = tu .unify_types (t1 , t2 , tst .TypeScriptBuiltinFactory ())
163
+ assert len (res ) == 1 and res [t2 .types [1 ]] == t1 .types [1 ]
164
+
165
+ t1 = tst .UnionType ([tst .NumberType (), tst .StringLiteralType ("foo" ), tst .StringType ()])
166
+ res = tu .unify_types (t1 , t2 , tst .TypeScriptBuiltinFactory ())
167
+ assert len (res ) == 1 and res [t2 .types [1 ]] == t1 .types [2 ]
168
+
169
+ t1 = tst .UnionType ([tst .NumberType (), tst .NumberLiteralType (100 ), tst .BooleanType (), tst .StringLiteralType ("foo" ), tst .StringType ()])
170
+
171
+ t_param1 = tp .TypeParameter ("T" , bound = tst .StringType ())
172
+ helper_union = tst .UnionType ([tst .BooleanType (), tst .StringType ()])
173
+ t_param2 = tp .TypeParameter ("G" , bound = helper_union )
174
+ t2 = tst .UnionType ([tst .NumberType (), t_param1 , t_param2 ])
175
+
176
+ # Unify t1: number | 100 | boolean | "foo" | string
177
+ # with t2: number | T extends string | G extends (boolean | string)
178
+ # Result should be {T: StringType, G: BooleanType}
179
+ res = tu .unify_types (t1 , t2 , tst .TypeScriptBuiltinFactory ())
180
+ assert (len (res ) == 2 and
181
+ res [t2 .types [1 ]] == t1 .types [4 ] and
182
+ res [t2 .types [2 ]] == t1 .types [2 ])
0 commit comments