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
// Utility typestypeEntity<T>=T;typeEager<T>={[PinkeyofT]-?: T[P]extendsEntity<infer U> ? Eager<U> : never;}&{[PinkeyofT]: T[P]extendsEntity<infer U> ? never : T[P];}// Database modelstypePerson={firstName: string;lastName: string;addresses?: Entity<Address[]>;}typeAddress={line1: string;town: string;telephoneLine: Entity<TelephoneLine>;}typeTelephoneLine={phoneNumber: string;features?: {hasBroadband: boolean;}}declarevarperson: Person;person.addresses[0]// EXPECTED - addresses is possibly undefineddeclarevareagerLoadedPerson: Eager<Person>eagerLoadedPerson.addresses[0].line1;// should work fine but doesn'teagerLoadedPerson.addresses[0].telephoneLine.phoneNumber;// should work fine but doesn'teagerLoadedPerson.addresses[0].telephoneLine.features.hasBroadband// EXPECTED - should give error because "features" is not an entity so the ? modifier should remain
Expected behavior:
I would expect the above code example to work (except for the EXPECTED errors as described). Well, not exactly the above code example, I appreciate the Eager type is a bit messed up.
Actual behavior:
Doesn't work - I can't seem to get a utility type Eager that will make all marked optional properties mandatory. In fact, forget marked properties. I can't even write an Eager type that will remove the optional modifier from all properties in the tree. For example the following example doesn't work either:
Related issues #23199 - I think this one is related but the filtering is finally achieved by using the property type - I can't seem to do that.
What I'm trying to achieve
We are using the sequelize ORM and we define some relationships on our models. When we query for our model, if we don't fetch the relationship then it will come back undefined. So we want our base type to represent that. However, sometimes we query for the whole tree so we'd like a utility type that will mark all those relation properties as being present.
The text was updated successfully, but these errors were encountered:
I think I found a PR which fixes the scenario where you can't make the whole tree mandatory: #23592
The following code works in 2.9.1 but not in 2.8.3:
typeEager<T>={[PinkeyofT]-?: Eager<T[P]>;}
But now I need a way to say only certain properties should be eager, and that others might still not be there (i.e. TelephoneLine.features is not an entity, it genuinely might not be there). I was planning to use marker types for this but it seems that won't work because of the nature of structural types. I tried defining Entity as T & { some kind of property or symbol tag } and if the property or symbol tag is optional (i.e. property?) then everything matches, and if it is mandatory then nothing matches.
TypeScript Version: 2.9.1-insiders.20180516
Search Terms:
mapped types
conditional mapped types
conditional required types
filter mapped types
Code
Expected behavior:
I would expect the above code example to work (except for the EXPECTED errors as described). Well, not exactly the above code example, I appreciate the Eager type is a bit messed up.
Actual behavior:
Doesn't work - I can't seem to get a utility type Eager that will make all marked optional properties mandatory. In fact, forget marked properties. I can't even write an Eager type that will remove the optional modifier from all properties in the tree. For example the following example doesn't work either:
Related issues
#23199 - I think this one is related but the filtering is finally achieved by using the property type - I can't seem to do that.
What I'm trying to achieve
We are using the sequelize ORM and we define some relationships on our models. When we query for our model, if we don't fetch the relationship then it will come back undefined. So we want our base type to represent that. However, sometimes we query for the whole tree so we'd like a utility type that will mark all those relation properties as being present.
The text was updated successfully, but these errors were encountered: