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
Describe the bug
When using morphism inside the function with generic types - it could not assign SourceType to SourceFromSchema<StrictSchema<D, SourceType>>
To Reproduce
Steps to reproduce the behavior:
Just run this file with ts-node locally. Also I commented specific error inside tranformWithGenerics function
import{createSchema,morphism,StrictSchema}from'morphism';interfaceTarget{target: string;}interfaceSource{source: string;}constsource: Source={source: '123',};constmap={target: 'source',};constschema=createSchema<Target,Source>(map);consttransformed=morphism(schema,source);console.log('transformed:',transformed);functiontransform(document: Source,m: StrictSchema<Target,Source>): Target{returnmorphism(m,document);}consttransformedWithFunction=transform(source,map);console.log('transformedWithFunction:',transformedWithFunction);functiontransformWithGenerics<T,S>(document: S,m: StrictSchema<T,S>): T{returnmorphism(m,document);// above line shows error:// Overload 2 of 6, '(schema: StrictSchema<T, S>, data: SourceFromSchema<StrictSchema<T, S>>): DestinationFromSchema<StrictSchema<T, S>>', gave the following error.// Argument of type 'S' is not assignable to parameter of type 'SourceFromSchema<StrictSchema<T, S>>'.}consttransformedWithGenerics=transformWithGenerics<Target,Source>(source,map);console.log('transformedWithGenerics:',transformedWithGenerics);
Expected behavior
I would expect it to properly work with generics, or am I missing something here?
Desktop (please complete the following information):
OS: MacOs Catalina 10.15.6
ts-node: 9.0.0
typescript: 3.8.3
The text was updated successfully, but these errors were encountered:
Hi @ceoworks ! Thank you for reporting this! I can confirm that's an issue. Morphism is not able to find the appropriate function overload. In fact you have to have access to an internal helper SourceFromSchema<StrictSchema<T, S>> that infer the source type from the schema (for those who does not type their schema but still want to have a target type inferred from their schema).
I wished this TypeScript proposal was available so that you would only have to provide the target type and the schema and morphism would enforce what the shape of the source should look like.
Please let me take some time to think about what is the best option to fix this because I think what you're trying to achieve is valuable and should be as simple as you suggested. I want to avoid externalizing the internal helper, sounds like it would bring confusion.
Describe the bug
When using morphism inside the function with generic types - it could not assign SourceType to SourceFromSchema<StrictSchema<D, SourceType>>
To Reproduce
Steps to reproduce the behavior:
Just run this file with
ts-node
locally. Also I commented specific error insidetranformWithGenerics
functionExpected behavior
I would expect it to properly work with generics, or am I missing something here?
Desktop (please complete the following information):
9.0.0
3.8.3
The text was updated successfully, but these errors were encountered: