Repository navigation
Inconsistent type inference when changing the order of type declarations (5.0 regression)Β #55666
Copy link
Copy link
Closed as not planned
Closed as not planned
Copy link
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed
Description
π Search Terms
N/A β not sure which part of the playground code causes the issue
π Version & Regression Information
- No bug on 4.9.5
- The bug can be observed starting from 5.0.4
β― Playground Link
π» Code
Apologies if the example is too long. I did my best to reduce it as much as possible, having encountered this bug in production.
type GraphQLNonSubSelectable =
| string
| Array<string | null | undefined>
| null
| undefined;
type GraphQLSubSelectable =
| GraphQLObject
| Array<GraphQLObject | null | undefined>;
type GraphQLValue = GraphQLNonSubSelectable | GraphQLSubSelectable;
type GraphQLObject = { [key: string]: GraphQLValue };
const TRANSFORMER = Symbol('TRANSFORMER');
interface SubSelectableFieldsSelectionOptions {
[TRANSFORMER]?: TransformerReference<object, GraphQLObject>;
}
type FieldsSelectionOptions<T> = T extends GraphQLSubSelectable
? SubSelectableFieldsSelectionOptions
: NonNullable<unknown>;
declare const TRANSFORMER_REFERENCE_TYPE: unique symbol;
declare const TRANSFORMER_REFERENCE_TRANSFORMER: unique symbol;
interface TransformerReference<
TTransformer extends object,
TType extends GraphQLObject,
> {
[TRANSFORMER_REFERENCE_TYPE]: TType;
[TRANSFORMER_REFERENCE_TRANSFORMER]: TTransformer;
}
type UnwrapSubSelectable<T extends GraphQLSubSelectable> = T extends Array<
infer V
>
? NonNullable<V>
: T;
interface ObjectWithTransformerOption<
TTransformer extends object,
TType extends GraphQLObject,
> {
[TRANSFORMER]: TransformerReference<TTransformer, TType>;
}
type FieldPathObject<T extends GraphQLObject> = {
[K in keyof T]?: T[K] extends infer V
? V extends GraphQLSubSelectable
? FieldPathObject<UnwrapSubSelectable<V>>
: NonNullable<unknown>
: never;
} & FieldsSelectionOptions<T>;
type FieldSubPathValue<V, TSubPath> = V extends GraphQLNonSubSelectable
? V
: V extends GraphQLSubSelectable
? TSubPath extends FieldPathObject<UnwrapSubSelectable<V>>
? V extends GraphQLObject
? FieldPathValue<UnwrapSubSelectable<V>, TSubPath>
: TSubPath extends ObjectWithTransformerOption<
infer TTransformer,
UnwrapSubSelectable<V>
>
? TTransformer
: unknown
: never
: never;
type FieldPathValue<
T extends GraphQLObject,
TPath extends FieldPathObject<T>,
> = {
[K in keyof TPath]: K extends keyof T
? FieldSubPathValue<T[K], TPath[K]>
: never;
}[keyof TPath];
interface FooterTransformer {
field: unknown;
}
type Entry = {
sys: NonNullable<unknown>;
};
type Query = {
footerCollection?: FooterCollection;
};
type ExternalLink = Entry & {
linkedFrom?: FooterCollection;
};
type Footer = Entry & {
footerLinksGroupCollection?: FooterLinksGroupCollection;
};
type FooterCollection = {
items: Footer[];
};
type FooterLinksGroup = Entry & {
groupLink?: ExternalLink | NonNullable<unknown>;
};
type FooterLinksGroupCollection = {
item: FooterLinksGroup;
};
type Path = {
footerCollection: {
items: {
[TRANSFORMER]: TransformerReference<FooterTransformer, Footer>;
};
};
};
type Item = FieldPathValue<Query, Path>;
declare const x: Item;
const y: FooterTransformer | undefined = x;π Actual behavior
- Observe that the example gives the following error:
TS2322: Type 'Footer[] | undefined' is not assignable to type 'FooterTransformer | undefined'.
Property 'field' is missing in type 'Footer[]' but required in type 'FooterTransformer'.
121 const y: FooterTransformer | undefined = x;
~
-
Put
type Footerbetweentype Queryandtype ExternalLink. Updated example -
Watch the error disappear.
π Expected behavior
Versions 4.9.5 an below correctly infer the types (produce no error). Changes to the order of type declarations do not affect the result.
Additional information about the issue
No response
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed
Activity
typescript-bot commentedon Sep 11, 2023
The change between v4.9.5 and v5.0.4 occurred at 3099385.
andrewbranch commentedon Sep 11, 2023
Caused by #52392
ahejlsberg commentedon May 29, 2024
The issue here is similar to that in #56291. The example requires resolving nested
FieldPathValueinstantiations more than three levels deep, and which point the type checker stops evaluating to avoid what appears to be infinite recursion. As I mention in #56291, we simply don't have a good way to determine that the instantiations eventually terminate. The reason the example works when types are reordered is that type IDs end up ordering differently and don't exhibit the same increasing ID pattern that causes termination.We're continuing to think about better ways to handle these scenarios.
typescript-bot commentedon Jun 1, 2024
This issue has been marked as "Design Limitation" and has seen no recent activity. It has been automatically closed for house-keeping purposes.