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
type inference, generics, array literals, array of array, literals, mongoose, relational schema extraction
Suggestion
The typing system can infer simple primitive type literals for function array calls.
I have already managed to get all the underlying plumbing working, the show stopper for this relationship type extract feature for framesworks like mongoose populate and deepPopulate.
The amount of time this relation typing feature would save us is hugh, if type inference could support it.
Repository of current work can be found here Current nearly working mongoose example
In the file 'index-types-manual-check.ts' a whole bunch of test cases around arrays literal type inference, this feature is requesting support for.
In this example it is clear that support for the request that void and & (intersection) type compound operator support, where void & something mutate into that something as requested in this feature request would be very usefully, as it would allow one to reduce void | x to x. as current extends can't be used with union types. void | string | number extends void | infer A | number ? 'T' : 'F'
[Type merging improvement => x & void = x since y & never = never, instead of u & void = illogical constraint(]#24852)
functionbasicTypeCapture<Textendsstring>(value : T) : T{return{}asany}constbasicTypeCaptureValue=basicTypeCapture('A')// Not an an array literal capture.functionArrayTypeCapture<TextendsArray<string>>(value : T) : T{return{}asany}// not an array literal capture// ['ItemA','ItemB']constarrayTypeCaptureValue=ArrayTypeCapture(['ItemA','ItemB']);// Good, but problem since can't covert a type into runtime value.constArrayTypeCaptureValueGeneric=ArrayTypeCapture<['ItemA','ItemB']>(['ItemA','ItemB']);ArrayTypeCaptureValueGeneric// Not an an array literal capture, expect 'T'functionArrayTypeCaptureEvaluate<T>(value : T) : Textends['ItemA'] ? 'T' :'F'|TextendsArray<'ItemA'> ? 'T' :'F'|Textends{'0' : 'ItemA'} ? 'T' :'F'{return{}asany}// Works, but problem since we can't convert a type into a runtime valueconstarrayTypeCaptureEvaluteArrayLiteralsSpecializeGeneric=ArrayTypeCaptureEvaluate<['ItemA']>(['ItemA']);typeEvaluateArrayLiteral<T>=Textends['ItemA'] ? 'T' :'F'|TextendsArray<'ItemA'> ? 'T' :'F'|Textends{'0' : 'ItemA'} ? 'T' :'F'// Not an an array literal capture, expect 'T'functionArrayTypeCaptureEvaluateUsingType<T>(value : T) : EvaluateArrayLiteral<T>{return{}asany}// ALso fails.constarrayTypeCaptureEvaluteArrayLiterals=ArrayTypeCaptureEvaluateUsingType(['ItemA']);// Works, but it is also a problem since we can't convert a type into a runtime value.constarrayTypeCaptureEvaluteArrayLiteralsSpecializeGeneric=ArrayTypeCaptureEvaluateUsingType<['ItemA']>(['ItemA']);// WorkstypeLeftSchemaPlain=ExtractRefSchema<LeftSchema,['ItemA']>constleftSchemaPlain : LeftSchemaPlain;//functionExtractRelationShipPath<T,P=never>(schema : T,value : P) : ExtractRefSchema<T,P>{return{}asany}constextractRelationShipPath=ExtractRelationShipPath({}asLeftSchema,['ItemA','Item1','Item2']);extractRelationShipPath.Item0.Item1.Item2functionCaptureArrayLiteralOfArrayLiteral<PextendsArray<Array<string>>>(path : P) : P{return{}asany}// FailsconstcaptureArrayLiteralOfArrayLiteral=CaptureArrayLiteralOfArrayLiteral([['ItemA','ItemB']]);// Works constcaptureArrayLiteralOfArrayLiteralSpesificGeneric=CaptureArrayLiteralOfArrayLiteral<[['ItemA','ItemB']]>([['ItemA','ItemB']]);functionCaptureArrayLiteralOfArrayLiteralAndEvaluate<PextendsArray<Array<string>>>(path : P) : PextendsArray<['ItemA','ItemB']> ? 'T' : 'F'{return{}asany}// WorksconstcaptureArrayLiteralOfArrayLiteralSpesificGenericA=CaptureArrayLiteralOfArrayLiteralAndEvaluate<[['ItemA','ItemB']]>([['ItemA','ItemB']]);// Fails, which is correctly working as expected.constcaptureArrayLiteralOfArrayLiteralSpesificGenericB=CaptureArrayLiteralOfArrayLiteralAndEvaluate<[['ItemA','Item']]>([['ItemA','Item']]);functionCapturePath<Sextends{},PextendsArray<Array<string>>>(path : P) : ExtractRelationshipType<S,P>{return{}asany}constCapturePathCaseA=CapturePath([['']]);// results in never [][], which we need to design our system to handle.CapturePathCaseA.
Bunch other test cases for referances
// Good only let primary keys.letcaseNoJoins : ExtractRelationshipType<LeftSchema,[never]>={a : 1,Item0 : {Item1 : {Item2 : '234'},Item11 : {Item12 : '12-A'},ItemA : {ItemB :
'AB-A'},b : true,c : "string",d : 234}}// Good only right hand keys, no fails positives.letcaseItem0JoinedNoFalsePositivesA : ExtractRelationshipType<LeftSchema,[['Item0'],['Item0','f'],['Item0','Item11']]>={a : 1,Item0 : {Item1 : {Item2 : '234'},Item11 : {Item12 : '12-A'},ItemA : {ItemB :
'AB-A'},b : true,c : "string",d : 234}}// Good only right hand keys, no fails positives.letcaseItem0JoinedNoFalsePositivesB : ExtractRelationshipType<LeftSchema,[['Item0'],['Item0','f'],['Item0','Item11'],['Item0','Item11','d']]>={a : 1,Item0 : {Item1 : {Item2 : '234'},Item11 : {Item12 : '12-A'},ItemA : {ItemB :
'AB-A'},b : true,c : "string",d : 234}}//Fails item0Item11.Item12 fails, should be left hand keyletcaseItem0Item11Item12JoinedNoFalsePositivesB : ExtractRelationshipType<LeftSchema,[['Item0'],['Item0','f'],['Item0','Item11'],['Item0','Item11','d']]>={a : 1,Item0 : {Item1 : {Item2 : '234'},Item11 : {Item12 : '12-B'},ItemA : {ItemB :
'AB-A'},b : true,c : "string",d : 234}}// Good no fail positives.letcaseItem0Item11Item12 : ExtractRelationshipType<LeftSchema,[['Item0'],['Item0','f'],['Item0','Item11'],['Item0','Item11','Item12']]>={a : 1,Item0 : {Item1 : {Item2 : '234'},Item11 : {Item12 : '12-B'},ItemA : {ItemB :
'AB-A'},b : true,c : "string",d : 234}}typeJoinPaths=[['Item0','e'],['Item0','Item1','Item2'],['Item0','Item11','Item12'],['Item0','ItemA','ItemB','B']];typeSchemaCaseAllJoined=ExtractRelationshipType<LeftSchema,JoinPaths>// Good all the right keys.letcaseAllJoined : SchemaCaseAllJoined={a : 1,Item0 : {Item1 : {Item2 : 234},Item11 : {Item12 : '12-B'},ItemA : {ItemB :
// 'AB-A'{a:234,B:'12B-B'},},b : true,c : "string",d : 234}}// Items a should be missign from Item0.ItemA.aletcaseAllJoinedFail : SchemaCaseAllJoined={Item0 : {Item1 : {Item2 : true},Item11 : {Item12 : '12-A'},ItemA : {ItemB : 'AB-A'// Missing the right hand side join//{a:234,B:'12B-B'}, },b : true,c : "string",d : 234}}
Checklist
My suggestion meets these guidelines:
This wouldn't be a breaking change in existing TypeScript / JavaScript code
This wouldn't change the runtime behavior of existing JavaScript code
This could be implemented without emitting different JS based on the types of the expressions
This isn't a runtime feature (e.g. new expression-level syntax)
The text was updated successfully, but these errors were encountered:
Search Terms
type inference, generics, array literals, array of array, literals, mongoose, relational schema extraction
Suggestion
The typing system can infer simple primitive type literals for function array calls.
I have already managed to get all the underlying plumbing working, the show stopper for this relationship type extract feature for framesworks like mongoose populate and deepPopulate.
The amount of time this relation typing feature would save us is hugh, if type inference could support it.
Repository of current work can be found here
Current nearly working mongoose example
In the file 'index-types-manual-check.ts' a whole bunch of test cases around arrays literal type inference, this feature is requesting support for.
In this example it is clear that support for the request that void and & (intersection) type compound operator support, where void & something mutate into that something as requested in this feature request would be very usefully, as it would allow one to reduce void | x to x. as current extends can't be used with union types. void | string | number extends void | infer A | number ? 'T' : 'F'
[Type merging improvement => x & void = x since y & never = never, instead of u & void = illogical constraint(]#24852)
Overview Examples Uses in Bigger picture
Basic schema
Populate Relation ship with populate
Mongoose example:
Current Schema example
Extraction function
Cases for function array type T generic inference
Bunch other test cases for referances
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: