Without creating a new interface / type, and without making all of the fields on my type definition optional, can I reference a type without including all of it's required fields?
Here's an example of the problem:
interface Test {
one: string;
two: string;
}
_.findWhere<Test, Test>(TestCollection, {
one: 'name'
});
For reference, the type definition for Underscore's .findWhere
method is this:
findWhere<T, U extends {}>(
list: _.List<T>,
properties: U): T;
I would like to use T
as the type for the properties parameter since it has the type information I want already, but trying to do this results in this typescript error:
Argument of type
'{ one: string; }'
is not assignable to parameter of type'Test'
. Property'two'
is missing in type'{ one: string; }'
.
Is there some extra syntax that will allow me to effectively make the one
and two
fields optional as needed? Something like the following:
_.findWhere<Test, Test?>(TestCollection, {
one: 'name'
});
I want autocomplete and for it to alert me when I'm using the wrong type information (e.x. strings when number is provided).
Does this exist in the language? Do I have to create a new type just in this case? Am I required to make all my fields optional?