Definitelytyped: [@types/react] useMemo's deps are should be optional

Created on 8 May 2019  路  2Comments  路  Source: DefinitelyTyped/DefinitelyTyped

  • [x] I tried using the @types/xxxx package and had problems.
  • [x] I tried using the latest stable version of tsc. https://www.npmjs.com/package/typescript
  • [x] I have a question that is inappropriate for StackOverflow. (Please ask any appropriate questions there).
  • [x] [Mention](https://github.com/blog/821-mention-somebody-they-re-notified) the authors (see Definitions by: in index.d.ts) so they can respond.
    Authors: @johnnyreilly @bbenezech @pzavolinsky @digiguru @ericanderson @tkrotoff @DovydasNavickas @onigoetz @theruther4d @guilhermehubner @ferdaber @jrakotoharisoa @pascaloliv @Hotell @franklixuefei @Jessidhia @pshrmn @saranshkataria @lukyth @eps1lon
// allow undefined, but don't make it optional as that is very likely a mistake
function useMemo<T>(factory: () => T, deps: DependencyList | undefined): T;

I think this is not correct. this function is designed to have optional deps param.
and also conflict with eslint-react-hooks. see the example below.

I wanted make editor model only once. so l wrote code like this.

image

and got this warning.

image

so I removed empty array and then I got this

image

I know that this model variable can be exist ouside of the function. but I'm curious that this function can be used without deps param in js but why cannot used like this in TS.

thanks for reading!

Most helpful comment

I'm not sure if this is already in a release version of the eslint plugin, but omitting the second argument is _also_ an error according to the eslint plugin.

See this check from the current eslint plugin code in master.


The reason why the argument cannot be omitted in the types is because, if you omit it, you are 99% of the time making a mistake. useCallback(x) without the second argument is _identical_ to just x (whatever x is) but with more CPU and memory overhead. useMemo(x) is identical to just x() unless x is a function declared outside the component; in which case you can probably just do useMemo(x, undefined), or useMemo(x, [x]).

All 2 comments

I'm not sure if this is already in a release version of the eslint plugin, but omitting the second argument is _also_ an error according to the eslint plugin.

See this check from the current eslint plugin code in master.


The reason why the argument cannot be omitted in the types is because, if you omit it, you are 99% of the time making a mistake. useCallback(x) without the second argument is _identical_ to just x (whatever x is) but with more CPU and memory overhead. useMemo(x) is identical to just x() unless x is a function declared outside the component; in which case you can probably just do useMemo(x, undefined), or useMemo(x, [x]).

thank's for the response. nice explaination! I'll close this issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fasatrix picture fasatrix  路  3Comments

jrmcdona picture jrmcdona  路  3Comments

JudeAlquiza picture JudeAlquiza  路  3Comments

Loghorn picture Loghorn  路  3Comments

csharpner picture csharpner  路  3Comments