Skip to content

[@types/react] type for useCallback breaks --noImplicitAny guarantees #52873

@gyzerok

Description

@gyzerok
// With `--noImplicitAny` set to true I expect the following code to error.
// $ExpectError
useCallback((implicitAnyHere) => '', [])

// And this one to compile
useCallback((good: string) => '', [])
// As well as this one
useCallback<(arg: string) => string>((good) => '', [])

Currently type definition for useCallback breaks the guarantees that --noImplicitAny gives. If developer does not specify a type for a function they are passing, all the arguments will default to any, which can easily lead to errors. I would argue that for the end developer/consumer this any type is implicit. It was totally unexpected for me and I discovered it by getting a runtime error which I would expect to be caught compile-time.

You can play around with it here.

Changing type definition to the following will fix the issue, however use of Function type is discouraged and comes as error when running npm test react.

function useCallback<T extends Function>(callback: T, deps: DependencyList);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions