-
Notifications
You must be signed in to change notification settings - Fork 13k
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 Footer
betweentype Query
andtype 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
Metadata
Metadata
Assignees
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed