Skip to content

Generics do not work properly with typescript #207

@ceoworks

Description

@ceoworks

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions