Skip to content

Commit 0a97562

Browse files
authored
Remove missingType when inferring from properties in EOPT (#57447)
1 parent 77bb090 commit 0a97562

8 files changed

+539
-2
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26179,7 +26179,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2617926179
for (const targetProp of properties) {
2618026180
const sourceProp = getPropertyOfType(source, targetProp.escapedName);
2618126181
if (sourceProp && !some(sourceProp.declarations, hasSkipDirectInferenceFlag)) {
26182-
inferFromTypes(getTypeOfSymbol(sourceProp), getTypeOfSymbol(targetProp));
26182+
inferFromTypes(
26183+
removeMissingType(getTypeOfSymbol(sourceProp), !!(sourceProp.flags & SymbolFlags.Optional)),
26184+
removeMissingType(getTypeOfSymbol(targetProp), !!(targetProp.flags & SymbolFlags.Optional)),
26185+
);
2618326186
}
2618426187
}
2618526188
}
@@ -30575,7 +30578,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3057530578
else if (t.flags & TypeFlags.StructuredType) {
3057630579
const prop = getPropertyOfType(t, name);
3057730580
if (prop) {
30578-
return isCircularMappedProperty(prop) ? undefined : removeMissingType(getTypeOfSymbol(prop), !!(prop && prop.flags & SymbolFlags.Optional));
30581+
return isCircularMappedProperty(prop) ? undefined : removeMissingType(getTypeOfSymbol(prop), !!(prop.flags & SymbolFlags.Optional));
3057930582
}
3058030583
if (isTupleType(t) && isNumericLiteralName(name) && +name >= 0) {
3058130584
const restType = getElementTypeOfSliceOfTupleType(t, t.target.fixedLength, /*endSkipCount*/ 0, /*writing*/ false, /*noReductions*/ true);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//// [tests/cases/compiler/inferenceExactOptionalProperties1.ts] ////
2+
3+
=== inferenceExactOptionalProperties1.ts ===
4+
type Test1 = { prop?: never } extends { prop?: infer T } ? T : false; // never
5+
>Test1 : Symbol(Test1, Decl(inferenceExactOptionalProperties1.ts, 0, 0))
6+
>prop : Symbol(prop, Decl(inferenceExactOptionalProperties1.ts, 0, 14))
7+
>prop : Symbol(prop, Decl(inferenceExactOptionalProperties1.ts, 0, 39))
8+
>T : Symbol(T, Decl(inferenceExactOptionalProperties1.ts, 0, 52))
9+
>T : Symbol(T, Decl(inferenceExactOptionalProperties1.ts, 0, 52))
10+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//// [tests/cases/compiler/inferenceExactOptionalProperties1.ts] ////
2+
3+
=== inferenceExactOptionalProperties1.ts ===
4+
type Test1 = { prop?: never } extends { prop?: infer T } ? T : false; // never
5+
>Test1 : never
6+
>prop : undefined
7+
>prop : T | undefined
8+
>false : false
9+
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
inferenceExactOptionalProperties2.ts(64,11): error TS2345: Argument of type '"alarm"' is not assignable to parameter of type '"counter"'.
2+
3+
4+
==== inferenceExactOptionalProperties2.ts (1 errors) ====
5+
type Values<T> = T[keyof T];
6+
7+
type EventObject = {
8+
type: string;
9+
};
10+
11+
interface ActorLogic<TEvent extends EventObject> {
12+
transition: (ev: TEvent) => unknown;
13+
}
14+
15+
type UnknownActorLogic = ActorLogic<never>;
16+
17+
interface ProvidedActor {
18+
src: string;
19+
logic: UnknownActorLogic;
20+
}
21+
22+
interface ActionFunction<TActor extends ProvidedActor> {
23+
(): void;
24+
_out_TActor?: TActor;
25+
}
26+
27+
interface AssignAction<TActor extends ProvidedActor> {
28+
(): void;
29+
_out_TActor?: TActor;
30+
}
31+
32+
interface MachineConfig<TActor extends ProvidedActor> {
33+
entry?: ActionFunction<TActor>;
34+
}
35+
36+
declare function assign<TActor extends ProvidedActor>(
37+
_: (spawn: (actor: TActor["src"]) => void) => {},
38+
): AssignAction<TActor>;
39+
40+
type ToProvidedActor<TActors extends Record<string, UnknownActorLogic>> =
41+
Values<{
42+
[K in keyof TActors & string]: {
43+
src: K;
44+
logic: TActors[K];
45+
};
46+
}>;
47+
48+
declare function setup<
49+
TActors extends Record<string, UnknownActorLogic> = {},
50+
>(implementations?: {
51+
actors?: { [K in keyof TActors]: TActors[K] };
52+
}): {
53+
createMachine: <
54+
const TConfig extends MachineConfig<ToProvidedActor<TActors>>,
55+
>(
56+
config: TConfig,
57+
) => void;
58+
};
59+
60+
declare const counterLogic: ActorLogic<{ type: "INCREMENT" }>;
61+
62+
// example usage
63+
setup({
64+
actors: { counter: counterLogic },
65+
}).createMachine({
66+
entry: assign((spawn) => {
67+
spawn("counter"); // ok
68+
spawn("alarm"); // error
69+
~~~~~~~
70+
!!! error TS2345: Argument of type '"alarm"' is not assignable to parameter of type '"counter"'.
71+
return {};
72+
}),
73+
});
74+
75+
// no provided actors, `assign` should still work
76+
setup().createMachine({
77+
entry: assign(() => ({})),
78+
});
79+
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
//// [tests/cases/compiler/inferenceExactOptionalProperties2.ts] ////
2+
3+
=== inferenceExactOptionalProperties2.ts ===
4+
type Values<T> = T[keyof T];
5+
>Values : Symbol(Values, Decl(inferenceExactOptionalProperties2.ts, 0, 0))
6+
>T : Symbol(T, Decl(inferenceExactOptionalProperties2.ts, 0, 12))
7+
>T : Symbol(T, Decl(inferenceExactOptionalProperties2.ts, 0, 12))
8+
>T : Symbol(T, Decl(inferenceExactOptionalProperties2.ts, 0, 12))
9+
10+
type EventObject = {
11+
>EventObject : Symbol(EventObject, Decl(inferenceExactOptionalProperties2.ts, 0, 28))
12+
13+
type: string;
14+
>type : Symbol(type, Decl(inferenceExactOptionalProperties2.ts, 2, 20))
15+
16+
};
17+
18+
interface ActorLogic<TEvent extends EventObject> {
19+
>ActorLogic : Symbol(ActorLogic, Decl(inferenceExactOptionalProperties2.ts, 4, 2))
20+
>TEvent : Symbol(TEvent, Decl(inferenceExactOptionalProperties2.ts, 6, 21))
21+
>EventObject : Symbol(EventObject, Decl(inferenceExactOptionalProperties2.ts, 0, 28))
22+
23+
transition: (ev: TEvent) => unknown;
24+
>transition : Symbol(ActorLogic.transition, Decl(inferenceExactOptionalProperties2.ts, 6, 50))
25+
>ev : Symbol(ev, Decl(inferenceExactOptionalProperties2.ts, 7, 15))
26+
>TEvent : Symbol(TEvent, Decl(inferenceExactOptionalProperties2.ts, 6, 21))
27+
}
28+
29+
type UnknownActorLogic = ActorLogic<never>;
30+
>UnknownActorLogic : Symbol(UnknownActorLogic, Decl(inferenceExactOptionalProperties2.ts, 8, 1))
31+
>ActorLogic : Symbol(ActorLogic, Decl(inferenceExactOptionalProperties2.ts, 4, 2))
32+
33+
interface ProvidedActor {
34+
>ProvidedActor : Symbol(ProvidedActor, Decl(inferenceExactOptionalProperties2.ts, 10, 43))
35+
36+
src: string;
37+
>src : Symbol(ProvidedActor.src, Decl(inferenceExactOptionalProperties2.ts, 12, 25))
38+
39+
logic: UnknownActorLogic;
40+
>logic : Symbol(ProvidedActor.logic, Decl(inferenceExactOptionalProperties2.ts, 13, 14))
41+
>UnknownActorLogic : Symbol(UnknownActorLogic, Decl(inferenceExactOptionalProperties2.ts, 8, 1))
42+
}
43+
44+
interface ActionFunction<TActor extends ProvidedActor> {
45+
>ActionFunction : Symbol(ActionFunction, Decl(inferenceExactOptionalProperties2.ts, 15, 1))
46+
>TActor : Symbol(TActor, Decl(inferenceExactOptionalProperties2.ts, 17, 25))
47+
>ProvidedActor : Symbol(ProvidedActor, Decl(inferenceExactOptionalProperties2.ts, 10, 43))
48+
49+
(): void;
50+
_out_TActor?: TActor;
51+
>_out_TActor : Symbol(ActionFunction._out_TActor, Decl(inferenceExactOptionalProperties2.ts, 18, 11))
52+
>TActor : Symbol(TActor, Decl(inferenceExactOptionalProperties2.ts, 17, 25))
53+
}
54+
55+
interface AssignAction<TActor extends ProvidedActor> {
56+
>AssignAction : Symbol(AssignAction, Decl(inferenceExactOptionalProperties2.ts, 20, 1))
57+
>TActor : Symbol(TActor, Decl(inferenceExactOptionalProperties2.ts, 22, 23))
58+
>ProvidedActor : Symbol(ProvidedActor, Decl(inferenceExactOptionalProperties2.ts, 10, 43))
59+
60+
(): void;
61+
_out_TActor?: TActor;
62+
>_out_TActor : Symbol(AssignAction._out_TActor, Decl(inferenceExactOptionalProperties2.ts, 23, 11))
63+
>TActor : Symbol(TActor, Decl(inferenceExactOptionalProperties2.ts, 22, 23))
64+
}
65+
66+
interface MachineConfig<TActor extends ProvidedActor> {
67+
>MachineConfig : Symbol(MachineConfig, Decl(inferenceExactOptionalProperties2.ts, 25, 1))
68+
>TActor : Symbol(TActor, Decl(inferenceExactOptionalProperties2.ts, 27, 24))
69+
>ProvidedActor : Symbol(ProvidedActor, Decl(inferenceExactOptionalProperties2.ts, 10, 43))
70+
71+
entry?: ActionFunction<TActor>;
72+
>entry : Symbol(MachineConfig.entry, Decl(inferenceExactOptionalProperties2.ts, 27, 55))
73+
>ActionFunction : Symbol(ActionFunction, Decl(inferenceExactOptionalProperties2.ts, 15, 1))
74+
>TActor : Symbol(TActor, Decl(inferenceExactOptionalProperties2.ts, 27, 24))
75+
}
76+
77+
declare function assign<TActor extends ProvidedActor>(
78+
>assign : Symbol(assign, Decl(inferenceExactOptionalProperties2.ts, 29, 1))
79+
>TActor : Symbol(TActor, Decl(inferenceExactOptionalProperties2.ts, 31, 24))
80+
>ProvidedActor : Symbol(ProvidedActor, Decl(inferenceExactOptionalProperties2.ts, 10, 43))
81+
82+
_: (spawn: (actor: TActor["src"]) => void) => {},
83+
>_ : Symbol(_, Decl(inferenceExactOptionalProperties2.ts, 31, 54))
84+
>spawn : Symbol(spawn, Decl(inferenceExactOptionalProperties2.ts, 32, 6))
85+
>actor : Symbol(actor, Decl(inferenceExactOptionalProperties2.ts, 32, 14))
86+
>TActor : Symbol(TActor, Decl(inferenceExactOptionalProperties2.ts, 31, 24))
87+
88+
): AssignAction<TActor>;
89+
>AssignAction : Symbol(AssignAction, Decl(inferenceExactOptionalProperties2.ts, 20, 1))
90+
>TActor : Symbol(TActor, Decl(inferenceExactOptionalProperties2.ts, 31, 24))
91+
92+
type ToProvidedActor<TActors extends Record<string, UnknownActorLogic>> =
93+
>ToProvidedActor : Symbol(ToProvidedActor, Decl(inferenceExactOptionalProperties2.ts, 33, 24))
94+
>TActors : Symbol(TActors, Decl(inferenceExactOptionalProperties2.ts, 35, 21))
95+
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
96+
>UnknownActorLogic : Symbol(UnknownActorLogic, Decl(inferenceExactOptionalProperties2.ts, 8, 1))
97+
98+
Values<{
99+
>Values : Symbol(Values, Decl(inferenceExactOptionalProperties2.ts, 0, 0))
100+
101+
[K in keyof TActors & string]: {
102+
>K : Symbol(K, Decl(inferenceExactOptionalProperties2.ts, 37, 5))
103+
>TActors : Symbol(TActors, Decl(inferenceExactOptionalProperties2.ts, 35, 21))
104+
105+
src: K;
106+
>src : Symbol(src, Decl(inferenceExactOptionalProperties2.ts, 37, 36))
107+
>K : Symbol(K, Decl(inferenceExactOptionalProperties2.ts, 37, 5))
108+
109+
logic: TActors[K];
110+
>logic : Symbol(logic, Decl(inferenceExactOptionalProperties2.ts, 38, 13))
111+
>TActors : Symbol(TActors, Decl(inferenceExactOptionalProperties2.ts, 35, 21))
112+
>K : Symbol(K, Decl(inferenceExactOptionalProperties2.ts, 37, 5))
113+
114+
};
115+
}>;
116+
117+
declare function setup<
118+
>setup : Symbol(setup, Decl(inferenceExactOptionalProperties2.ts, 41, 5))
119+
120+
TActors extends Record<string, UnknownActorLogic> = {},
121+
>TActors : Symbol(TActors, Decl(inferenceExactOptionalProperties2.ts, 43, 23))
122+
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
123+
>UnknownActorLogic : Symbol(UnknownActorLogic, Decl(inferenceExactOptionalProperties2.ts, 8, 1))
124+
125+
>(implementations?: {
126+
>implementations : Symbol(implementations, Decl(inferenceExactOptionalProperties2.ts, 45, 2))
127+
128+
actors?: { [K in keyof TActors]: TActors[K] };
129+
>actors : Symbol(actors, Decl(inferenceExactOptionalProperties2.ts, 45, 21))
130+
>K : Symbol(K, Decl(inferenceExactOptionalProperties2.ts, 46, 14))
131+
>TActors : Symbol(TActors, Decl(inferenceExactOptionalProperties2.ts, 43, 23))
132+
>TActors : Symbol(TActors, Decl(inferenceExactOptionalProperties2.ts, 43, 23))
133+
>K : Symbol(K, Decl(inferenceExactOptionalProperties2.ts, 46, 14))
134+
135+
}): {
136+
createMachine: <
137+
>createMachine : Symbol(createMachine, Decl(inferenceExactOptionalProperties2.ts, 47, 5))
138+
139+
const TConfig extends MachineConfig<ToProvidedActor<TActors>>,
140+
>TConfig : Symbol(TConfig, Decl(inferenceExactOptionalProperties2.ts, 48, 18))
141+
>MachineConfig : Symbol(MachineConfig, Decl(inferenceExactOptionalProperties2.ts, 25, 1))
142+
>ToProvidedActor : Symbol(ToProvidedActor, Decl(inferenceExactOptionalProperties2.ts, 33, 24))
143+
>TActors : Symbol(TActors, Decl(inferenceExactOptionalProperties2.ts, 43, 23))
144+
145+
>(
146+
config: TConfig,
147+
>config : Symbol(config, Decl(inferenceExactOptionalProperties2.ts, 50, 4))
148+
>TConfig : Symbol(TConfig, Decl(inferenceExactOptionalProperties2.ts, 48, 18))
149+
150+
) => void;
151+
};
152+
153+
declare const counterLogic: ActorLogic<{ type: "INCREMENT" }>;
154+
>counterLogic : Symbol(counterLogic, Decl(inferenceExactOptionalProperties2.ts, 55, 13))
155+
>ActorLogic : Symbol(ActorLogic, Decl(inferenceExactOptionalProperties2.ts, 4, 2))
156+
>type : Symbol(type, Decl(inferenceExactOptionalProperties2.ts, 55, 40))
157+
158+
// example usage
159+
setup({
160+
>setup({ actors: { counter: counterLogic },}).createMachine : Symbol(createMachine, Decl(inferenceExactOptionalProperties2.ts, 47, 5))
161+
>setup : Symbol(setup, Decl(inferenceExactOptionalProperties2.ts, 41, 5))
162+
163+
actors: { counter: counterLogic },
164+
>actors : Symbol(actors, Decl(inferenceExactOptionalProperties2.ts, 58, 7))
165+
>counter : Symbol(counter, Decl(inferenceExactOptionalProperties2.ts, 59, 11))
166+
>counterLogic : Symbol(counterLogic, Decl(inferenceExactOptionalProperties2.ts, 55, 13))
167+
168+
}).createMachine({
169+
>createMachine : Symbol(createMachine, Decl(inferenceExactOptionalProperties2.ts, 47, 5))
170+
171+
entry: assign((spawn) => {
172+
>entry : Symbol(entry, Decl(inferenceExactOptionalProperties2.ts, 60, 18))
173+
>assign : Symbol(assign, Decl(inferenceExactOptionalProperties2.ts, 29, 1))
174+
>spawn : Symbol(spawn, Decl(inferenceExactOptionalProperties2.ts, 61, 17))
175+
176+
spawn("counter"); // ok
177+
>spawn : Symbol(spawn, Decl(inferenceExactOptionalProperties2.ts, 61, 17))
178+
179+
spawn("alarm"); // error
180+
>spawn : Symbol(spawn, Decl(inferenceExactOptionalProperties2.ts, 61, 17))
181+
182+
return {};
183+
}),
184+
});
185+
186+
// no provided actors, `assign` should still work
187+
setup().createMachine({
188+
>setup().createMachine : Symbol(createMachine, Decl(inferenceExactOptionalProperties2.ts, 47, 5))
189+
>setup : Symbol(setup, Decl(inferenceExactOptionalProperties2.ts, 41, 5))
190+
>createMachine : Symbol(createMachine, Decl(inferenceExactOptionalProperties2.ts, 47, 5))
191+
192+
entry: assign(() => ({})),
193+
>entry : Symbol(entry, Decl(inferenceExactOptionalProperties2.ts, 69, 23))
194+
>assign : Symbol(assign, Decl(inferenceExactOptionalProperties2.ts, 29, 1))
195+
196+
});
197+

0 commit comments

Comments
 (0)