-
Notifications
You must be signed in to change notification settings - Fork 13k
Closed
Labels
UnactionableThere isn't something we can do with this issueThere isn't something we can do with this issue
Description
TypeScript Version: 3.8.3 - 3.9.5 (probably more)
Code
// suppose that IRL this type does something more useful
type MakeOptional<TObject extends object> = {
// the "extends infer TValue" is crucial - without it, everything works as expected
[TKey in keyof TObject]?: TObject[TKey] extends infer TValue
? TValue
: never;
};
type BaseType = { a: number; b: { c: string } };
type ConcreteType = { a: 42; b: { c: 'stuff' }; };
declare const value: ConcreteType;
declare function doNothing<
// Here, ConcreteType would entail the same result
TValue extends BaseType,
TMappedValue extends MakeOptional<TValue>
>(
value: TValue,
mappedValue: TMappedValue,
): {
value: TValue;
mappedValue: TMappedValue;
};
const result = doNothing(value, {
a: 42,
});
Expected behavior:
The inferred type of result should be
{
value: ConcreteType;
mappedValue: {
a: number
};
}
Actual behavior:
The inferred type of result is
{
value: ConcreteType;
mappedValue: MakeOptional<ConcreteType>;
}
There is a workaround: If you pass the argument for the parameter mappedValue
with as const
, the compiler will instead infer the concrete type. However, if you do this you lose code completion for the fields of mappedValue
. So basically, you are left with what I would consider another bug/shortcoming of the compiler.
Related Issues:
Metadata
Metadata
Assignees
Labels
UnactionableThere isn't something we can do with this issueThere isn't something we can do with this issue