Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generics do not work properly with typescript #207

Open
ceoworks opened this issue Nov 28, 2020 · 3 comments
Open

Generics do not work properly with typescript #207

ceoworks opened this issue Nov 28, 2020 · 3 comments

Comments

@ceoworks
Copy link

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';

interface Target {
	target: string;
}

interface Source {
	source: string;
}

const source: Source = {
	source: '123',
};

const map = {
	target: 'source',
};

const schema = createSchema<Target, Source>(map);
const transformed = morphism(schema, source);
console.log('transformed:', transformed);

function transform(document: Source, m: StrictSchema<Target, Source>): Target {
	return morphism(m, document);
}

const transformedWithFunction = transform(source, map);
console.log('transformedWithFunction:', transformedWithFunction);

function transformWithGenerics<T, S>(document: S, m: StrictSchema<T, S>): T {
	return morphism(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>>'.
}

const transformedWithGenerics = 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
@emyann
Copy link
Member

emyann commented Dec 1, 2020

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.

@obiwabrakenobi
Copy link

any news on this issue?

@PedroMiotti
Copy link

Any updates on this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants