You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Recursive mapped type inference not working as expected #24318
typeEntity<T>=T&{_: "Entity"};typeMyNestedType={something0: number;level1?: {something1: number;level2?: {something2: number;level3?: {something3: number;property: string;};};};level1Array?: Entity<{something1: number;level2Array?: Entity<{something2: number;level3Array?: Entity<{something3: number;property: string;}[]>;}[]>;}[]>;};typeEverythingPresent<T>={[PinkeyofT]-?: EverythingPresent<T[P]>};declarevareverythingPresent: EverythingPresent<MyNestedType>;everythingPresent.level1;everythingPresent.level1.level2;everythingPresent.level1.level2.level3;everythingPresent.level1Array[0].something1;everythingPresent.level1Array[0].level2Array[0].something2;everythingPresent.level1Array[0].level2Array[0].level3Array[0].something3;typeOnlyEntitiesPresent<T>={[PinkeyofT]-?: T[P]extendsEntity<infer U>|undefined
? Entity<UextendsArray<infer R>
? Array<OnlyEntitiesPresent<R>>
: OnlyEntitiesPresent<U>>
: T[P]};declarevaronlyArraysPresent: OnlyEntitiesPresent<MyNestedType>;onlyArraysPresent.level1;onlyArraysPresent.level1.level2;onlyArraysPresent.level1.level2.level3;// This errors as expectedonlyArraysPresent.level1Array[0].something1;onlyArraysPresent.level1Array[0].level2Array[0].something2;// This should not erroronlyArraysPresent.level1Array[0].level2Array[0].level3Array[0].something3;// This should not error
Expected behavior:
I should only get an error where expected (1 line)
Actual behavior:
I get errors on the last 2 lines as well
Just to note - in 2.8.3 I also get errors for the last 3 lines of everythingPresent - so some improvements have been made but it's still not as expected
changed the title [-]Recursive generic parameter inference not working[/-][+]Recursive mapped type inference not working as expected[/+]on May 22, 2018
Andy (Andrewkraft) (@Andy-MS) Thanks, that's cleared things up a bit... it seems that the Array is a special case where things aren't quite working as expected... the examples with Node all work fine now
Andy (Andrewkraft) (@Andy-MS) so I've basically replicated the issue I was originally facing. If you had a moment to take a look, here's the code example that I'm having issues with.
// EntitiestypeOrder={id: number;customer?: Entity<Customer>;lineItems?: Entity<OrderLineItem[]>;};typeCustomer={name: string;bestFriend?: Entity<Customer>;legacyData?: {registered: boolean;};};typeOrderLineItem={quantity: number;options?: Entity<OrderLineItemOption[]>;item?: Entity<Item>;};typeOrderLineItemOption={colour: string;};typeItem={description: string;};// Type helperstypePropertiesWithoutNevers<T,U>={[PinkeyofT]: T[P]extendsU ? never : P}[keyofT];typeMandatory<T>={[PinkeyofT]-?: T[P]};typeEntity<T>=T&{_: "Entity"};typeEagerLoadedRelations<TEntity>=Pick<Mandatory<TEntity>,PropertiesWithoutNevers<{[PinkeyofTEntity]-?: TEntity[P]extendsEntity<infer TRelation>|undefined
? Entity<TRelationextendsArray<infer TOneToMany> ? Array<Eager<TOneToMany>> : Eager<TRelation>>
: never},TEntity>>;typeNormalFields<TEntity>=Pick<TEntity,PropertiesWithoutNevers<{[PinkeyofTEntity]-?: TEntity[P]extendsEntity<infer U>|undefined ? never : TEntity[P]},TEntity>>;typeEager<T>=EagerLoadedRelations<T>&NormalFields<T>;// Without Eager<T> - everything works as expecteddeclarevarorder: Order;order.id;// works as expectedorder.customer.name;// error as expectedorder.lineItems[0].quantity;// error as expected// With Eager<T> - Eager<T> doesn't seem to apply deeper than 1 leveldeclarevareagerOrder: Eager<Order>;eagerOrder.id;// works as expectedeagerOrder.customer.name;// works as expectedeagerOrder.customer.bestFriend.name;// ERROR - doesn't work - bestFriend should be eager loadedeagerOrder.customer.legacyData.registered;// errors as expectedeagerOrder.lineItems[0].quantity;// works as expectedeagerOrder.lineItems[0].item.description;// ERROR - doesn't work - items should be eager loadedeagerOrder.lineItems[0].options[0].colour;// ERROR - doesn't work - options should be eager loaded
Activity
massimonewsuk commentedon May 22, 2018
Here are some examples where it works as expected
[-]Recursive generic parameter inference not working[/-][+]Recursive mapped type inference not working as expected[/+]massimonewsuk commentedon May 22, 2018
Andy (Andrewkraft) (@Andy-MS) Thanks, that's cleared things up a bit... it seems that the
Arrayis a special case where things aren't quite working as expected... the examples withNodeall work fine nowmassimonewsuk commentedon May 22, 2018
You're right - I could have sworn I tried that and it never helped.
I've updated my original code example to what I actually wanted to achieve.
It seems to work fine in 2.9.1 so I'm closing the issue. Sorry for taking your time!
massimonewsuk commentedon May 22, 2018
Andy (Andrewkraft) (@Andy-MS) so I've basically replicated the issue I was originally facing. If you had a moment to take a look, here's the code example that I'm having issues with.