Skip to content

Generic constrained to function: parameter is incorrectly inferred as any #41461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
OliverJAsh opened this issue Nov 9, 2020 · 2 comments · May be fixed by #58910
Open

Generic constrained to function: parameter is incorrectly inferred as any #41461

OliverJAsh opened this issue Nov 9, 2020 · 2 comments · May be fixed by #58910
Labels
Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Milestone

Comments

@OliverJAsh
Copy link
Contributor

TypeScript Version: 4.0.2

Search Terms:

Code

declare function useCallback<T extends (...args: any[]) => any>(
    callback: T,
): T;
const test: ((x: string) => void) | undefined = useCallback((x) => {
    x; // ❌ type is inferred as `any`, expected `string`
});

For context, this typing of useCallback is extracted from the React types.

Expected behavior: see above

Actual behavior: see above

Playground Link: https://www.typescriptlang.org/play?#code/CYUwxgNghgTiAEAzArgOzAFwJYHtX2QGcQBhKCCAIyjAGsAeAFXhAA8MRVhD4AKAOkGwA5oQBc8KKgCeAbQC6ASngBeAHySZa3gCh4++GHJUatCYwA0OxeYDcOsHkIZ4HZxN69WE5zCyphZXV4ADccLGBlAB8CLhBEfxBgVQJiMgpqOk9WII0Abz0DVlt4AHpS+EAZcldpAAcELB5-RBAYOGSoHgADKWkuixZWeswk+C7ff2EunQBfRVsgA

Related Issues:

@RyanCavanaugh RyanCavanaugh added Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript labels Nov 10, 2020
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Nov 10, 2020
@RyanCavanaugh
Copy link
Member

The problem here is the contextual type being undefined | (x: string) => void instead of just (x: string) => void - that defeats our matching logic for using the return type as an inference candidate for T

@Andarist
Copy link
Contributor

It's likely related to this: #50719 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants