Skip to content

Commit 540e84d

Browse files
committed
Avoid resolving source prop type when the target is unknown/any
1 parent d88d3a4 commit 540e84d

14 files changed

+835
-1
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24057,6 +24057,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2405724057
function isPropertySymbolTypeRelated(sourceProp: Symbol, targetProp: Symbol, getTypeOfSourceProperty: (sym: Symbol) => Type, reportErrors: boolean, intersectionState: IntersectionState): Ternary {
2405824058
const targetIsOptional = strictNullChecks && !!(getCheckFlags(targetProp) & CheckFlags.Partial);
2405924059
const effectiveTarget = addOptionality(getNonMissingTypeOfSymbol(targetProp), /*isProperty*/ false, targetIsOptional);
24060+
// source could resolve to `any` and that's not related to `unknown` target under strict subtype relation
24061+
if (effectiveTarget.flags & (relation === strictSubtypeRelation ? TypeFlags.Any : TypeFlags.AnyOrUnknown)) {
24062+
return Ternary.True;
24063+
}
2406024064
const effectiveSource = getTypeOfSourceProperty(sourceProp);
2406124065
return isRelatedTo(effectiveSource, effectiveTarget, RecursionFlags.Both, reportErrors, /*headMessage*/ undefined, intersectionState);
2406224066
}

tests/baselines/reference/multiline.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
=== Performance Stats ===
44
Type Count: 2,500
5-
Instantiation count: 2,500
5+
Instantiation count: 1,000
66

77
=== a.ts ===
88
export const texts: string[] = [];
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
//// [tests/cases/compiler/noCircularitySelfReferentialGetter1.ts] ////
2+
3+
=== noCircularitySelfReferentialGetter1.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/61659
5+
6+
interface ZodType {
7+
>ZodType : Symbol(ZodType, Decl(noCircularitySelfReferentialGetter1.ts, 0, 0))
8+
9+
optional: "true" | "false";
10+
>optional : Symbol(ZodType.optional, Decl(noCircularitySelfReferentialGetter1.ts, 2, 19))
11+
12+
output: any;
13+
>output : Symbol(ZodType.output, Decl(noCircularitySelfReferentialGetter1.ts, 3, 29))
14+
}
15+
16+
interface ZodString extends ZodType {
17+
>ZodString : Symbol(ZodString, Decl(noCircularitySelfReferentialGetter1.ts, 5, 1))
18+
>ZodType : Symbol(ZodType, Decl(noCircularitySelfReferentialGetter1.ts, 0, 0))
19+
20+
optional: "false";
21+
>optional : Symbol(ZodString.optional, Decl(noCircularitySelfReferentialGetter1.ts, 7, 37))
22+
23+
output: string;
24+
>output : Symbol(ZodString.output, Decl(noCircularitySelfReferentialGetter1.ts, 8, 20))
25+
}
26+
27+
type ZodShape = Record<string, any>;
28+
>ZodShape : Symbol(ZodShape, Decl(noCircularitySelfReferentialGetter1.ts, 10, 1))
29+
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
30+
31+
type Prettify<T> = { [K in keyof T]: T[K] } & {};
32+
>Prettify : Symbol(Prettify, Decl(noCircularitySelfReferentialGetter1.ts, 12, 36))
33+
>T : Symbol(T, Decl(noCircularitySelfReferentialGetter1.ts, 13, 14))
34+
>K : Symbol(K, Decl(noCircularitySelfReferentialGetter1.ts, 13, 22))
35+
>T : Symbol(T, Decl(noCircularitySelfReferentialGetter1.ts, 13, 14))
36+
>T : Symbol(T, Decl(noCircularitySelfReferentialGetter1.ts, 13, 14))
37+
>K : Symbol(K, Decl(noCircularitySelfReferentialGetter1.ts, 13, 22))
38+
39+
type InferObjectType<Shape extends ZodShape> = Prettify<
40+
>InferObjectType : Symbol(InferObjectType, Decl(noCircularitySelfReferentialGetter1.ts, 13, 49))
41+
>Shape : Symbol(Shape, Decl(noCircularitySelfReferentialGetter1.ts, 14, 21))
42+
>ZodShape : Symbol(ZodShape, Decl(noCircularitySelfReferentialGetter1.ts, 10, 1))
43+
>Prettify : Symbol(Prettify, Decl(noCircularitySelfReferentialGetter1.ts, 12, 36))
44+
{
45+
[k in keyof Shape as Shape[k] extends { optional: "true" }
46+
>k : Symbol(k, Decl(noCircularitySelfReferentialGetter1.ts, 16, 5))
47+
>Shape : Symbol(Shape, Decl(noCircularitySelfReferentialGetter1.ts, 14, 21))
48+
>Shape : Symbol(Shape, Decl(noCircularitySelfReferentialGetter1.ts, 14, 21))
49+
>k : Symbol(k, Decl(noCircularitySelfReferentialGetter1.ts, 16, 5))
50+
>optional : Symbol(optional, Decl(noCircularitySelfReferentialGetter1.ts, 16, 43))
51+
52+
? k
53+
>k : Symbol(k, Decl(noCircularitySelfReferentialGetter1.ts, 16, 5))
54+
55+
: never]?: Shape[k]["output"];
56+
>Shape : Symbol(Shape, Decl(noCircularitySelfReferentialGetter1.ts, 14, 21))
57+
>k : Symbol(k, Decl(noCircularitySelfReferentialGetter1.ts, 16, 5))
58+
59+
} & {
60+
[k in keyof Shape as Shape[k] extends { optional: "true" }
61+
>k : Symbol(k, Decl(noCircularitySelfReferentialGetter1.ts, 20, 5))
62+
>Shape : Symbol(Shape, Decl(noCircularitySelfReferentialGetter1.ts, 14, 21))
63+
>Shape : Symbol(Shape, Decl(noCircularitySelfReferentialGetter1.ts, 14, 21))
64+
>k : Symbol(k, Decl(noCircularitySelfReferentialGetter1.ts, 20, 5))
65+
>optional : Symbol(optional, Decl(noCircularitySelfReferentialGetter1.ts, 20, 43))
66+
67+
? never
68+
: k]: Shape[k]["output"];
69+
>k : Symbol(k, Decl(noCircularitySelfReferentialGetter1.ts, 20, 5))
70+
>Shape : Symbol(Shape, Decl(noCircularitySelfReferentialGetter1.ts, 14, 21))
71+
>k : Symbol(k, Decl(noCircularitySelfReferentialGetter1.ts, 20, 5))
72+
}
73+
>;
74+
interface ZodObject<T extends ZodShape> extends ZodType {
75+
>ZodObject : Symbol(ZodObject, Decl(noCircularitySelfReferentialGetter1.ts, 24, 2))
76+
>T : Symbol(T, Decl(noCircularitySelfReferentialGetter1.ts, 25, 20))
77+
>ZodShape : Symbol(ZodShape, Decl(noCircularitySelfReferentialGetter1.ts, 10, 1))
78+
>ZodType : Symbol(ZodType, Decl(noCircularitySelfReferentialGetter1.ts, 0, 0))
79+
80+
optional: "false";
81+
>optional : Symbol(ZodObject.optional, Decl(noCircularitySelfReferentialGetter1.ts, 25, 57))
82+
83+
output: InferObjectType<T>;
84+
>output : Symbol(ZodObject.output, Decl(noCircularitySelfReferentialGetter1.ts, 26, 20))
85+
>InferObjectType : Symbol(InferObjectType, Decl(noCircularitySelfReferentialGetter1.ts, 13, 49))
86+
>T : Symbol(T, Decl(noCircularitySelfReferentialGetter1.ts, 25, 20))
87+
}
88+
89+
interface ZodOptional<T extends ZodType> extends ZodType {
90+
>ZodOptional : Symbol(ZodOptional, Decl(noCircularitySelfReferentialGetter1.ts, 28, 1))
91+
>T : Symbol(T, Decl(noCircularitySelfReferentialGetter1.ts, 30, 22))
92+
>ZodType : Symbol(ZodType, Decl(noCircularitySelfReferentialGetter1.ts, 0, 0))
93+
>ZodType : Symbol(ZodType, Decl(noCircularitySelfReferentialGetter1.ts, 0, 0))
94+
95+
optional: "true";
96+
>optional : Symbol(ZodOptional.optional, Decl(noCircularitySelfReferentialGetter1.ts, 30, 58))
97+
98+
output: T["output"] | undefined;
99+
>output : Symbol(ZodOptional.output, Decl(noCircularitySelfReferentialGetter1.ts, 31, 19))
100+
>T : Symbol(T, Decl(noCircularitySelfReferentialGetter1.ts, 30, 22))
101+
}
102+
103+
declare function object<T extends ZodShape>(shape: T): ZodObject<T>;
104+
>object : Symbol(object, Decl(noCircularitySelfReferentialGetter1.ts, 33, 1))
105+
>T : Symbol(T, Decl(noCircularitySelfReferentialGetter1.ts, 35, 24))
106+
>ZodShape : Symbol(ZodShape, Decl(noCircularitySelfReferentialGetter1.ts, 10, 1))
107+
>shape : Symbol(shape, Decl(noCircularitySelfReferentialGetter1.ts, 35, 44))
108+
>T : Symbol(T, Decl(noCircularitySelfReferentialGetter1.ts, 35, 24))
109+
>ZodObject : Symbol(ZodObject, Decl(noCircularitySelfReferentialGetter1.ts, 24, 2))
110+
>T : Symbol(T, Decl(noCircularitySelfReferentialGetter1.ts, 35, 24))
111+
112+
declare function string(): ZodString;
113+
>string : Symbol(string, Decl(noCircularitySelfReferentialGetter1.ts, 35, 68))
114+
>ZodString : Symbol(ZodString, Decl(noCircularitySelfReferentialGetter1.ts, 5, 1))
115+
116+
declare function optional<T extends ZodType>(schema: T): ZodOptional<T>;
117+
>optional : Symbol(optional, Decl(noCircularitySelfReferentialGetter1.ts, 36, 37))
118+
>T : Symbol(T, Decl(noCircularitySelfReferentialGetter1.ts, 37, 26))
119+
>ZodType : Symbol(ZodType, Decl(noCircularitySelfReferentialGetter1.ts, 0, 0))
120+
>schema : Symbol(schema, Decl(noCircularitySelfReferentialGetter1.ts, 37, 45))
121+
>T : Symbol(T, Decl(noCircularitySelfReferentialGetter1.ts, 37, 26))
122+
>ZodOptional : Symbol(ZodOptional, Decl(noCircularitySelfReferentialGetter1.ts, 28, 1))
123+
>T : Symbol(T, Decl(noCircularitySelfReferentialGetter1.ts, 37, 26))
124+
125+
const Category = object({
126+
>Category : Symbol(Category, Decl(noCircularitySelfReferentialGetter1.ts, 39, 5))
127+
>object : Symbol(object, Decl(noCircularitySelfReferentialGetter1.ts, 33, 1))
128+
129+
name: string(),
130+
>name : Symbol(name, Decl(noCircularitySelfReferentialGetter1.ts, 39, 25))
131+
>string : Symbol(string, Decl(noCircularitySelfReferentialGetter1.ts, 35, 68))
132+
133+
get parent() {
134+
>parent : Symbol(parent, Decl(noCircularitySelfReferentialGetter1.ts, 40, 17))
135+
136+
return optional(Category);
137+
>optional : Symbol(optional, Decl(noCircularitySelfReferentialGetter1.ts, 36, 37))
138+
>Category : Symbol(Category, Decl(noCircularitySelfReferentialGetter1.ts, 39, 5))
139+
140+
},
141+
});
142+
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
//// [tests/cases/compiler/noCircularitySelfReferentialGetter1.ts] ////
2+
3+
=== noCircularitySelfReferentialGetter1.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/61659
5+
6+
interface ZodType {
7+
optional: "true" | "false";
8+
>optional : "true" | "false"
9+
> : ^^^^^^^^^^^^^^^^
10+
11+
output: any;
12+
>output : any
13+
}
14+
15+
interface ZodString extends ZodType {
16+
optional: "false";
17+
>optional : "false"
18+
> : ^^^^^^^
19+
20+
output: string;
21+
>output : string
22+
> : ^^^^^^
23+
}
24+
25+
type ZodShape = Record<string, any>;
26+
>ZodShape : ZodShape
27+
> : ^^^^^^^^
28+
29+
type Prettify<T> = { [K in keyof T]: T[K] } & {};
30+
>Prettify : { [K in keyof T]: T[K]; }
31+
> : ^^^ ^^^^^^^^^^^^^^^^^^^^^
32+
33+
type InferObjectType<Shape extends ZodShape> = Prettify<
34+
>InferObjectType : { [k in keyof Shape as Shape[k] extends { optional: "true"; } ? k : never]?: Shape[k]["output"] | undefined; } & { [k_1 in keyof Shape as Shape[k_1] extends { optional: "true"; } ? never : k_1]: Shape[k_1]["output"]; } extends infer T ? { [K in keyof T]: ({ [k in keyof Shape as Shape[k] extends { optional: "true"; } ? k : never]?: Shape[k]["output"] | undefined; } & { [k_1 in keyof Shape as Shape[k_1] extends { optional: "true"; } ? never : k_1]: Shape[k_1]["output"]; })[K]; } : never
35+
> : ^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
36+
{
37+
[k in keyof Shape as Shape[k] extends { optional: "true" }
38+
>optional : "true"
39+
> : ^^^^^^
40+
41+
? k
42+
: never]?: Shape[k]["output"];
43+
} & {
44+
[k in keyof Shape as Shape[k] extends { optional: "true" }
45+
>optional : "true"
46+
> : ^^^^^^
47+
48+
? never
49+
: k]: Shape[k]["output"];
50+
}
51+
>;
52+
interface ZodObject<T extends ZodShape> extends ZodType {
53+
optional: "false";
54+
>optional : "false"
55+
> : ^^^^^^^
56+
57+
output: InferObjectType<T>;
58+
>output : { [k in keyof T as T[k] extends { optional: "true"; } ? k : never]?: T[k]["output"] | undefined; } & { [k_1 in keyof T as T[k_1] extends { optional: "true"; } ? never : k_1]: T[k_1]["output"]; } extends infer T_1 ? { [K in keyof T_1]: ({ [k in keyof T as T[k] extends { optional: "true"; } ? k : never]?: T[k]["output"] | undefined; } & { [k_1 in keyof T as T[k_1] extends { optional: "true"; } ? never : k_1]: T[k_1]["output"]; })[K]; } : never
59+
> : ^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
60+
}
61+
62+
interface ZodOptional<T extends ZodType> extends ZodType {
63+
optional: "true";
64+
>optional : "true"
65+
> : ^^^^^^
66+
67+
output: T["output"] | undefined;
68+
>output : T["output"] | undefined
69+
> : ^^^^^^^^^^^^^^^^^^^^^^^
70+
}
71+
72+
declare function object<T extends ZodShape>(shape: T): ZodObject<T>;
73+
>object : <T extends ZodShape>(shape: T) => ZodObject<T>
74+
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^
75+
>shape : T
76+
> : ^
77+
78+
declare function string(): ZodString;
79+
>string : () => ZodString
80+
> : ^^^^^^
81+
82+
declare function optional<T extends ZodType>(schema: T): ZodOptional<T>;
83+
>optional : <T extends ZodType>(schema: T) => ZodOptional<T>
84+
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^
85+
>schema : T
86+
> : ^
87+
88+
const Category = object({
89+
>Category : ZodObject<{ name: ZodString; readonly parent: ZodOptional<ZodObject<any>>; }>
90+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
91+
>object({ name: string(), get parent() { return optional(Category); },}) : ZodObject<{ name: ZodString; readonly parent: ZodOptional<ZodObject<any>>; }>
92+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
93+
>object : <T extends ZodShape>(shape: T) => ZodObject<T>
94+
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^
95+
>{ name: string(), get parent() { return optional(Category); },} : { name: ZodString; readonly parent: ZodOptional<ZodObject<{ name: ZodString; readonly parent: ZodOptional<ZodObject<any>>; }>>; }
96+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
97+
98+
name: string(),
99+
>name : ZodString
100+
> : ^^^^^^^^^
101+
>string() : ZodString
102+
> : ^^^^^^^^^
103+
>string : () => ZodString
104+
> : ^^^^^^
105+
106+
get parent() {
107+
>parent : ZodOptional<ZodObject<{ name: ZodString; readonly parent: ZodOptional<ZodObject<any>>; }>>
108+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
109+
110+
return optional(Category);
111+
>optional(Category) : ZodOptional<ZodObject<{ name: ZodString; readonly parent: ZodOptional<ZodObject<any>>; }>>
112+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
113+
>optional : <T extends ZodType>(schema: T) => ZodOptional<T>
114+
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^
115+
>Category : ZodObject<{ name: ZodString; readonly parent: ZodOptional<ZodObject<any>>; }>
116+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
117+
118+
},
119+
});
120+

0 commit comments

Comments
 (0)