🔎 Search Terms
type parameter default
type parameter inference
default type argument
generic default
not assignable to parameter of type 'undefined'
tsgo generic
ts7 generic
🕗 Version & Regression Information
- This changed between versions 6.0.3 and 7.0.0
⏯ Playground Link
https://www.typescriptlang.org/play/?ts=6.0.3#code/PTAEBUGVQdgOgAxwEygE4FMDmmDOuBLAewDsAuUXABwFc1ibdQAiEogF1AEN8CsSuAIwA2GUOyKgqXNFwC2GdhjSgiAM3EBPKmIDkNEgBMMagiQyHdzALAAoEHZCgAgqCwZz9AMag1Br+zEJKAA7gAWRLhi7Npi0rIKSioABuDJoGE83KAAVMZqXDTC7DncRqAETIwWoIIcYY5gALQVwVxSMvKKylo6oAAUghhhZobZXqSGBIGkXMK9GACUADRlho2gLWYV7Ew5ZmrKmIalmOx0wTE6G8KRGEzsmZz5hcWhYR7iH6Bec-OZVB0JCYZkIxnGf0EXC8AGsvlxOJUdlFhBpHhgNjIsDQFCROOoyvUeu5PAQfH4SAEggMMHAsHBQAAlDDQ9i6KpRADCkOhMMWcEaGygoAAbIg4ABmChsUBHIhoBmgCDQeBIVBgeAARkQTWMADcKHKVKQvmJkoYEVx0licR52ALbHZjF5hDIxBSqSbDuwvB80AAeAAiAD4Bl41FgKAYYWwQiRFhQAApoIhySoYIPBux2K5iRlEGhKXD+xmhgC8TNlAA8lEYmLoq7pQAB+UAAb1AAEkAHIUDsG0AkHFDFQAX1WAHkAKrgCi4dj0EhYUCj0DSjB65TZ+xgbsT8AAUWlkjOFwWZTY7AR1KaLXR6EUZ9zFRBJEOaGOa1AuJmwIgDo9X9qwwLxCwwAAxRRfRLata0MJh50XLBVnAUAK3zMDi1LUN+jsJUUzAihGWWPDaiIQxNGbChUIwGsPHgpkQPlQx-V0HtdFWLgSE0UNW3AABtNju10ABdNdBw3LdbEWdtSNPNBgm9X1lH9Gi6LrRiJjQFjdGncAOLKHiWwgQS9NE8TwGDfoOwIpRVjqCiV0WOxR23Z1XUwXx-CA6puWEYQoVhVTYPophwO8oIcK8QRqNWYwqFwKMSBjIg434kSEwgUAnHuYQzHYXVKiEUQmjy8xt1oqh5U4CZgRqwQ0NAXyeVhfp+gtK8+1AAchzkEcnLQ0M2zkx8FOA0ClEgn0wn6BsDI6rhnNsJUnCVNb1o2zalQAPV27bSKcYUYEND95WVZAJQAFgAVgoZw0GxH9VDRWJQF0ftpWHZQAG4VybJFL24Xh+GKjFluysBNokDoEm6Y0Xr6fQjBMMwLF0B0VrAfoCDgWlQFSdIQiyQGXiKJQxmhjCi1Yxtg1WXBJHvWqpl-OZxCIIg4XRA7IYKYQolqWRKTCTZQ2ScxNzQZJOPKcXJJUAAfJrkdMcxDHSJoxYMfJUfVpbx1AdKlqFb4cjwMnSmfJF5wIfyfnlTAAlaMjHm65RCFIJh+mTVN039RCzCwYMVneMkGh3SgIhCB5viNe2FCYNQUzkbIplwX5tJqA5lA8Lw4h4RK7Eq6r7bqn4PlhJNk79gOl3LH5BGs7qKE1Jy7CAA
💻 Code
// TS 7.0.2 regression: spurious "not assignable to parameter of type 'undefined'"
//
// A generic function whose type parameter `T` has a *default* and is used both
// - in a parameter type (behind a conditional type), and
// - in its *inferred* return type
// loses that default when the call happens inside a callback that is itself the
// argument of another generic function (e.g. React's useCallback).
//
// TS 6.0.3: no error. TS 7.0.2 / 7.1.0-dev: error on the `data` argument.
declare function fetcher<D> (cfg: unknown): Promise<D>
type Routes<R> = R extends 'x' ? { IN: { v: number }, OUT: string } : never
// NOTE: no return type annotation -- the return type is inferred and mentions T.
function executeFetch<R extends string, T = Routes<R>> (
route: R,
body?: T extends Record<'IN', any> ? T['IN'] : never
) {
return fetcher<T extends Record<'OUT', any> ? T['OUT'] : T>({ route, body })
}
declare function useCallback<T extends Function> (cb: T, deps: unknown[]): T // eslint-disable-line
export const cb = useCallback((data: { v: number }) => {
return executeFetch('x', data)
// ^^^^
// TS 7: error TS2345: Argument of type '{ v: number; }' is not assignable
// to parameter of type 'undefined'.
// (i.e. `T` was not defaulted to Routes<'x'>, so the conditional took the
// false branch -> `never`, and `never | undefined` -> `undefined`)
}, [])
// The *result* type is still correct in both versions (Promise<string>), which
// shows the error comes from a discarded inference pass:
export const check: Promise<string> = cb({ v: 1 })
🙁 Actual behavior
Error: Argument of type '{ v: number; }' is not assignable to parameter of type 'undefined'.
🙂 Expected behavior
The proper type should be inferred on the function generic
Additional information about the issue
This issue was discovered in a large codeback and Claude Opus 5 was used to identify the issue and construct the reproduction.
It's worth noting that the TypeScript Playground does not support TS7 so this issue needs to be debugged locally (simply run tsc on the code and compare the errors (or lackthereof) between versions 6 and 7)
🔎 Search Terms
type parameter default
type parameter inference
default type argument
generic default
not assignable to parameter of type 'undefined'
tsgo generic
ts7 generic
🕗 Version & Regression Information
⏯ Playground Link
https://www.typescriptlang.org/play/?ts=6.0.3#code/PTAEBUGVQdgOgAxwEygE4FMDmmDOuBLAewDsAuUXABwFc1ibdQAiEogF1AEN8CsSuAIwA2GUOyKgqXNFwC2GdhjSgiAM3EBPKmIDkNEgBMMagiQyHdzALAAoEHZCgAgqCwZz9AMag1Br+zEJKAA7gAWRLhi7Npi0rIKSioABuDJoGE83KAAVMZqXDTC7DncRqAETIwWoIIcYY5gALQVwVxSMvKKylo6oAAUghhhZobZXqSGBIGkXMK9GACUADRlho2gLWYV7Ew5ZmrKmIalmOx0wTE6G8KRGEzsmZz5hcWhYR7iH6Bec-OZVB0JCYZkIxnGf0EXC8AGsvlxOJUdlFhBpHhgNjIsDQFCROOoyvUeu5PAQfH4SAEggMMHAsHBQAAlDDQ9i6KpRADCkOhMMWcEaGygoAAbIg4ABmChsUBHIhoBmgCDQeBIVBgeAARkQTWMADcKHKVKQvmJkoYEVx0licR52ALbHZjF5hDIxBSqSbDuwvB80AAeAAiAD4Bl41FgKAYYWwQiRFhQAApoIhySoYIPBux2K5iRlEGhKXD+xmhgC8TNlAA8lEYmLoq7pQAB+UAAb1AAEkAHIUDsG0AkHFDFQAX1WAHkAKrgCi4dj0EhYUCj0DSjB65TZ+xgbsT8AAUWlkjOFwWZTY7AR1KaLXR6EUZ9zFRBJEOaGOa1AuJmwIgDo9X9qwwLxCwwAAxRRfRLata0MJh50XLBVnAUAK3zMDi1LUN+jsJUUzAihGWWPDaiIQxNGbChUIwGsPHgpkQPlQx-V0HtdFWLgSE0UNW3AABtNju10ABdNdBw3LdbEWdtSNPNBgm9X1lH9Gi6LrRiJjQFjdGncAOLKHiWwgQS9NE8TwGDfoOwIpRVjqCiV0WOxR23Z1XUwXx-CA6puWEYQoVhVTYPophwO8oIcK8QRqNWYwqFwKMSBjIg434kSEwgUAnHuYQzHYXVKiEUQmjy8xt1oqh5U4CZgRqwQ0NAXyeVhfp+gtK8+1AAchzkEcnLQ0M2zkx8FOA0ClEgn0wn6BsDI6rhnNsJUnCVNb1o2zalQAPV27bSKcYUYEND95WVZAJQAFgAVgoZw0GxH9VDRWJQF0ftpWHZQAG4VybJFL24Xh+GKjFluysBNokDoEm6Y0Xr6fQjBMMwLF0B0VrAfoCDgWlQFSdIQiyQGXiKJQxmhjCi1Yxtg1WXBJHvWqpl-OZxCIIg4XRA7IYKYQolqWRKTCTZQ2ScxNzQZJOPKcXJJUAAfJrkdMcxDHSJoxYMfJUfVpbx1AdKlqFb4cjwMnSmfJF5wIfyfnlTAAlaMjHm65RCFIJh+mTVN039RCzCwYMVneMkGh3SgIhCB5viNe2FCYNQUzkbIplwX5tJqA5lA8Lw4h4RK7Eq6r7bqn4PlhJNk79gOl3LH5BGs7qKE1Jy7CAA
💻 Code
🙁 Actual behavior
Error:
Argument of type '{ v: number; }' is not assignable to parameter of type 'undefined'.🙂 Expected behavior
The proper type should be inferred on the function generic
Additional information about the issue
This issue was discovered in a large codeback and Claude Opus 5 was used to identify the issue and construct the reproduction.
It's worth noting that the TypeScript Playground does not support TS7 so this issue needs to be debugged locally (simply run tsc on the code and compare the errors (or lackthereof) between versions 6 and 7)