dojo / core

:rocket: Dojo 2 - language helpers and utilities.
http://dojo.io
Other
213 stars 62 forks source link

Use Partial for the assign/mixin typings #291

Closed agubler closed 7 years ago

agubler commented 7 years ago

The sources for assign/mixin functions should be typed so that subsequent sources can contain attributes that are not included in the first source. At the moment compile errors occurs if the second source expands the keys compared to the initial source.

Code:

const baseObject = {
    foo: 'bar'
};

lang.assign({}, baseObject, { baz: 'foo' });

Expected:

Compile successfully

Actual:

error TS2453: The type argument for type parameter 'U' cannot be inferred from the usage. Consider specifying the type arguments explicitly. Type argument candidate '{ foo: string; }' is not a valid type argument because it is not a supertype of candidate '{ baz: string; }'. Object literal may only specify known properties, and 'baz' does not exist in type '{ foo: string; }'.

smhigley commented 7 years ago

Related error:

Code:

type TypeA = {
   foo: string;
   bar: string;
};
const thingA = {
   foo: 'foo',
   bar: 'bar'
};
const thingB = {
   bar: 'baz'
};
const result: TypeA = lang.assign({}, thingA, thingB);

Expected: Compile Successfully

Actual: The type argument for type parameter 'U' cannot be inferred from the usage. Consider specifying the type arguments explicitly. Type argument candidate 'TypeA' is not a valid type argument because it is not a supertype of candidate '{ bar: string; }'. Property 'foo' is missing in type '{ bar: string; }'.

kitsonk commented 7 years ago

This is pretty important, now that we have the types to model it. Bumping the priority on it.