Skip to content

Commit f1f578f

Browse files
committed
fix: property access on union types with generic conditionals
1 parent 233f392 commit f1f578f

File tree

5 files changed

+120
-1
lines changed

5 files changed

+120
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15496,7 +15496,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1549615496
let mergedInstantiations = false;
1549715497
for (const current of containingType.types) {
1549815498
const type = getApparentType(current);
15499-
if (!(isErrorType(type) || type.flags & TypeFlags.Never)) {
15499+
if (!(isErrorType(type) || type.flags & TypeFlags.Never || type.flags & TypeFlags.Any)) {
1550015500
const prop = getPropertyOfType(type, name, skipObjectFunctionPropertyAugment);
1550115501
const modifiers = prop ? getDeclarationModifierFlagsFromSymbol(prop) : 0;
1550215502
if (prop) {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//// [tests/cases/compiler/propertyAccessOnUnionWithGenericConditional.ts] ////
2+
3+
//// [propertyAccessOnUnionWithGenericConditional.ts]
4+
type InCommon = { common: string };
5+
6+
type CondWithAny<K extends string | number> =
7+
K extends number ? any : { two: string };
8+
9+
type UnionWithAny<K extends string | number> =
10+
InCommon | (CondWithAny<K> & InCommon);
11+
12+
function testWithAny<K extends string | number>(k: K) {
13+
const val = {} as UnionWithAny<K>;
14+
val.common;
15+
}
16+
17+
//// [propertyAccessOnUnionWithGenericConditional.js]
18+
function testWithAny(k) {
19+
var val = {};
20+
val.common;
21+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//// [tests/cases/compiler/propertyAccessOnUnionWithGenericConditional.ts] ////
2+
3+
=== propertyAccessOnUnionWithGenericConditional.ts ===
4+
type InCommon = { common: string };
5+
>InCommon : Symbol(InCommon, Decl(propertyAccessOnUnionWithGenericConditional.ts, 0, 0))
6+
>common : Symbol(common, Decl(propertyAccessOnUnionWithGenericConditional.ts, 0, 17))
7+
8+
type CondWithAny<K extends string | number> =
9+
>CondWithAny : Symbol(CondWithAny, Decl(propertyAccessOnUnionWithGenericConditional.ts, 0, 35))
10+
>K : Symbol(K, Decl(propertyAccessOnUnionWithGenericConditional.ts, 2, 17))
11+
12+
K extends number ? any : { two: string };
13+
>K : Symbol(K, Decl(propertyAccessOnUnionWithGenericConditional.ts, 2, 17))
14+
>two : Symbol(two, Decl(propertyAccessOnUnionWithGenericConditional.ts, 3, 28))
15+
16+
type UnionWithAny<K extends string | number> =
17+
>UnionWithAny : Symbol(UnionWithAny, Decl(propertyAccessOnUnionWithGenericConditional.ts, 3, 43))
18+
>K : Symbol(K, Decl(propertyAccessOnUnionWithGenericConditional.ts, 5, 18))
19+
20+
InCommon | (CondWithAny<K> & InCommon);
21+
>InCommon : Symbol(InCommon, Decl(propertyAccessOnUnionWithGenericConditional.ts, 0, 0))
22+
>CondWithAny : Symbol(CondWithAny, Decl(propertyAccessOnUnionWithGenericConditional.ts, 0, 35))
23+
>K : Symbol(K, Decl(propertyAccessOnUnionWithGenericConditional.ts, 5, 18))
24+
>InCommon : Symbol(InCommon, Decl(propertyAccessOnUnionWithGenericConditional.ts, 0, 0))
25+
26+
function testWithAny<K extends string | number>(k: K) {
27+
>testWithAny : Symbol(testWithAny, Decl(propertyAccessOnUnionWithGenericConditional.ts, 6, 41))
28+
>K : Symbol(K, Decl(propertyAccessOnUnionWithGenericConditional.ts, 8, 21))
29+
>k : Symbol(k, Decl(propertyAccessOnUnionWithGenericConditional.ts, 8, 48))
30+
>K : Symbol(K, Decl(propertyAccessOnUnionWithGenericConditional.ts, 8, 21))
31+
32+
const val = {} as UnionWithAny<K>;
33+
>val : Symbol(val, Decl(propertyAccessOnUnionWithGenericConditional.ts, 9, 7))
34+
>UnionWithAny : Symbol(UnionWithAny, Decl(propertyAccessOnUnionWithGenericConditional.ts, 3, 43))
35+
>K : Symbol(K, Decl(propertyAccessOnUnionWithGenericConditional.ts, 8, 21))
36+
37+
val.common;
38+
>val.common : Symbol(common, Decl(propertyAccessOnUnionWithGenericConditional.ts, 0, 17))
39+
>val : Symbol(val, Decl(propertyAccessOnUnionWithGenericConditional.ts, 9, 7))
40+
>common : Symbol(common, Decl(propertyAccessOnUnionWithGenericConditional.ts, 0, 17))
41+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//// [tests/cases/compiler/propertyAccessOnUnionWithGenericConditional.ts] ////
2+
3+
=== propertyAccessOnUnionWithGenericConditional.ts ===
4+
type InCommon = { common: string };
5+
>InCommon : InCommon
6+
> : ^^^^^^^^
7+
>common : string
8+
> : ^^^^^^
9+
10+
type CondWithAny<K extends string | number> =
11+
>CondWithAny : CondWithAny<K>
12+
> : ^^^^^^^^^^^^^^
13+
14+
K extends number ? any : { two: string };
15+
>two : string
16+
> : ^^^^^^
17+
18+
type UnionWithAny<K extends string | number> =
19+
>UnionWithAny : UnionWithAny<K>
20+
> : ^^^^^^^^^^^^^^^
21+
22+
InCommon | (CondWithAny<K> & InCommon);
23+
24+
function testWithAny<K extends string | number>(k: K) {
25+
>testWithAny : <K extends string | number>(k: K) => void
26+
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^^^^^
27+
>k : K
28+
> : ^
29+
30+
const val = {} as UnionWithAny<K>;
31+
>val : UnionWithAny<K>
32+
> : ^^^^^^^^^^^^^^^
33+
>{} as UnionWithAny<K> : UnionWithAny<K>
34+
> : ^^^^^^^^^^^^^^^
35+
>{} : {}
36+
> : ^^
37+
38+
val.common;
39+
>val.common : string
40+
> : ^^^^^^
41+
>val : UnionWithAny<K>
42+
> : ^^^^^^^^^^^^^^^
43+
>common : string
44+
> : ^^^^^^
45+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
type InCommon = { common: string };
2+
3+
type CondWithAny<K extends string | number> =
4+
K extends number ? any : { two: string };
5+
6+
type UnionWithAny<K extends string | number> =
7+
InCommon | (CondWithAny<K> & InCommon);
8+
9+
function testWithAny<K extends string | number>(k: K) {
10+
const val = {} as UnionWithAny<K>;
11+
val.common;
12+
}

0 commit comments

Comments
 (0)