Repository navigation
bug: generic type argument not inferred from function argument #30975
Description
Activity
try removing the default type param for
R. Sometimes setting defaults can interfere with inferrenceUmeayo 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 commented
on Apr 17, 2019 MemberMore actionsThe problem is effectively here:
function addSchedulePattern< T extends typeof DateAdapter, R extends Schedule<T> = Schedule<T>, >( date: T['date'], schedule: R ): R { return schedule; }
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:
type DateFor<S> = S extends Schedule<infer R> ? R["date"] : never; function addSchedulePattern<S extends Schedule<typeof DateAdapter>>(date: DateFor<S>, schedule: S): S { return schedule; }
which properly errors in both cases.
- addedQuestionAn issue which isn't directly actionable in codeAn issue which isn't directly actionable in code
on 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:
type DateInput<T extends typeof DateAdapter> = T['date'] | InstanceType<T> | DateTime; interface ITypedWithDateAdapter<T extends typeof DateAdapter> {} type DateAdapterFor<O> = O extends ITypedWithDateAdapter<infer A> ? A : never;
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>function addSchedulePattern<S extends Schedule<typeof DateAdapter>>( pattern: Pattern, date: DateInput<DateAdapterFor<S>>, schedule: S, options: { dateAdapter?: DateAdapterFor<S> } = {} ): S { // `Schedule<T>#add(): Schedule<T>` return schedule.add('rdate', date); // this throws the error }
I can easily work around this error with
return schedule.add('rdate', date) as S2) The following also throws an error for
schedule.add()function addSchedulePattern<S extends Schedule<typeof DateAdapter>>( pattern: Pattern, date: DateInput<DateAdapterFor<S>>, schedule: S, options: { dateAdapter?: DateAdapterFor<S> } = {} ): S { // Where `Schedule<T>#add(type: 'rrule', value: Rule<T>): Schedule<T>` return schedule.add( 'rrule', new Rule(buildRecurrencePattern(pattern, date, options), options), // <- PROBLEM IS HERE ); }
Basically:
- In the function definition,
scheduleis being typed asSchedule<typeof DateAdapter>. Schedule<T>#add()is expecting the second argument to beRule<T>, so it is expectingRule<typeof DateAdapter>- However,
new Rule()is being typed correctly asRule<DateAdapterFor<S>> - The fact that
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.- In the function definition,
Perhaps these errors are different issues?
RyanCavanaugh commented
on Apr 18, 2019 MemberMore actionsThis is obviously strange because it's saying
Schedule<typeof DateAdapter>is not assignable toS extends Schedule<typeof DateAdapter>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.
Interesting. Thanks for the information!
I don't see anything here that looks like a TypeScript bug.
Gotcha
- locked as resolved and limited conversation to collaborators
on Oct 21, 2025
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:
Expected behavior:
It is expected that
addSchedulePattern()can infer the type argumentTfrom the providedscheduleargument of typeR extends Schedule<T>.Actual behavior:
addSchedulePattern()always resolves the type argumentTastypeof DateAdapterPlayground Link:
https://stackblitz.com/edit/typescript-kjji5c
Related Issues:
#30505 may be related