Repository navigation
Closed
Labels
QuestionAn issue which isn't directly actionable in code
Description
TypeScript Version: 3.4.3
Search Terms:
generic type argument inference
Code
A reproduction of the error can be found in this StackBlitz example: https://stackblitz.com/edit/typescript-kjji5c.
The stackblitz code is copied below:
import moment from 'moment';
/**
* The "test()" function below shows a type problem.
* Ideally, typescript would be able to use the `schedule`
* argument to `addSchedulePattern()` to infer the
* `addSchedulePattern()` type argument `T`. However,
* typescript fails to do this.
*
* - "test2", below, should throw a type error when passed
* `regularDate`, but it doesn't.
*
* - when I explicitly add typing to `addSchedulePattern()` in
* "test4", then typescript correctly catches the type error
*/
function test(
schedule: Schedule<typeof MomentDateAdapter>,
momentDate: moment.Moment,
regularDate: Date
) {
const test = addSchedulePattern(
momentDate,
schedule,
)
const test2 = addSchedulePattern(
regularDate, // <- this should error but doesn't
schedule,
)
const test3 = addSchedulePattern<typeof MomentDateAdapter>(
momentDate,
schedule,
)
const test4 = addSchedulePattern<typeof MomentDateAdapter>(
regularDate, // <- this properly errors
schedule,
)
}
class DateAdapter {
static readonly date: unknown;
readonly date: unknown;
}
class MomentDateAdapter extends DateAdapter {
static readonly date: moment.Moment;
readonly date: moment.Moment;
}
class Schedule<T extends typeof DateAdapter> { }
function addSchedulePattern<
T extends typeof DateAdapter,
R extends Schedule<T> = Schedule<T>,
>(
date: T['date'],
schedule: R
): R {
return schedule;
}Expected behavior:
It is expected that addSchedulePattern() can infer the type argument T from the provided schedule argument of type R extends Schedule<T>.
Actual behavior:
addSchedulePattern() always resolves the type argument T as typeof DateAdapter
Playground Link:
https://stackblitz.com/edit/typescript-kjji5c
Related Issues:
#30505 may be related
Metadata
Metadata
Assignees
Labels
QuestionAn issue which isn't directly actionable in code
Activity
Dudeonyx commentedon Apr 17, 2019
try removing the default type param for
R. Sometimes setting defaults can interfere with inferrencejorroll commentedon Apr 17, 2019
Umeayo Onyekachukwu (@Dudeonyx) good idea, but it doesn't make a difference. You can test by opening up the stackblitz example, making the change, and then hovering over
addSchedulePatternintest2to see how typescript has typedaddSchedulePattern().I'm not going to update the stackblitz example with this change because it would force me to explicitly type the
dateargument intest4, eliminating the error typescript shows. This would make it harder to "see" the problem.RyanCavanaugh commentedon Apr 17, 2019
The problem is effectively here:
You don't really have an inference site for
There, which is very problematic - TS is going to be falling back to the constraint for the type parameter any time it isn't explicitly specified. Compounding that is the fact that you have two type parameters but really only one floating type in play, so the inferences being made are 100% valid.What you want is this definition:
which properly errors in both cases.
jorroll commentedon Apr 17, 2019
Ryan Cavanaugh (@RyanCavanaugh) thanks! That's very helpful to know. This example was simplified (perhaps too much so) and trying out your suggestion creates two new problems:
Assuming these types:
1) The following throws the error
Type 'Schedule<typeof DateAdapter>' is not assignable to type 'S'This is obviously strange because it's saying
Schedule<typeof DateAdapter>is not assignable toS extends Schedule<typeof DateAdapter>I can easily work around this error with
return schedule.add('rdate', date) as S2) The following also throws an error for
schedule.add()Basically:
scheduleis being typed asSchedule<typeof DateAdapter>.Schedule<T>#add()is expecting the second argument to beRule<T>, so it is expectingRule<typeof DateAdapter>new Rule()is being typed correctly asRule<DateAdapterFor<S>>Rule<DateAdapterFor<S>>is notRule<typeof DateAdapter>seems to be causing an error.I doubt you'll want to look at it, but FYI the original code for these examples is here: https://gitlab.com/john.carroll.p/rschedule/blob/master/packages/rule-tools/src/lib/schedule.ts#L53-66
Update
I can work around error (2) by asserting
schedule as Schedule<DateAdapterFor<S>>, but then I run into (1) again.jorroll commentedon Apr 17, 2019
Perhaps these errors are different issues?
RyanCavanaugh commentedon Apr 18, 2019
This is a correct error; see https://stackoverflow.com/questions/46980763/why-cant-i-return-a-generic-t-to-satisfy-a-partialt for a similar example. In general for a
T extends U, you cannot assign aUto aTbecauseTmay be instantiated with a more-specific type at runtime.I don't see anything here that looks like a TypeScript bug.
jorroll commentedon Apr 19, 2019
Interesting. Thanks for the information!
Gotcha