#Typing return type for react's `useMemo()`

14 messages · Page 1 of 1 (latest)

acoustic timberBOT
sudden adder
#

!ts

acoustic timberBOT
#
import { useMemo } from "react";
//                      ^^^^^^^
// Cannot find module 'react' or its corresponding type declarations.

type Foo = { a: string };

useMemo<Foo>(() => {
  return { a: "", b: "" };
}, []);

useMemo((): Foo => {
  return { a: "", b: "" };
//                ^^^^^
// Type '{ a: string; b: string; }' is not assignable to type 'Foo'.
//   Object literal may only specify known properties, and 'b' does not exist in type 'Foo'.
}, []);
sudden adder
#

Erm. not sure about the messages

#

I don't have them

#

I wonder why the first variant allows for b

#

Ideas?

#

It has a type as:

#
function useMemo<T>(factory: () => T, deps: DependencyList | undefined): T;
#

Oh... wait

#

I stupid

#

Nevermind

#

It is generic! Damn