0

Trying to get the hang of typescript and it's weird syntaxes.

How do you read the following in your head?

const [user, setUser] = useState<IUser>({name: 'Jon'});

Am I right to say useState takes <IUser> as an input?

Or useState takes an input of <IUser>?

example stolen from: https://stackoverflow.com/a/53650561/9780778

hackerl33t
  • 2,117
  • 3
  • 14
  • 21
  • I say "useState holds IUsers". (One at a time, of course, but that's how standard English works.) – Tom Mar 09 '22 at 02:48
  • I go with "user state is an IUser". I wouldn't necessarily call it "takes" just because that's not quite how you should be thinking of it. – Zachary Haber Mar 09 '22 at 02:57
  • I'd say "Create a state variable with the type `IUser`". But that's not how I'd read every other function that is being called with a *generic*. You have to read the type definition of the function to understand what it does with the generic. – Amiratak88 Mar 09 '22 at 03:18
  • "`useState` operates on values with the type `IUser`." – kelsny Mar 09 '22 at 05:24
  • in this case, `useState` takes `IUser` as a *type* input to narrow down its generic signature, this type in this case happens to be the type of the first argument. In other functions it may be for input's nested type, or type inside a function argument, or the type of the output... – Phu Ngo Mar 10 '22 at 09:31

0 Answers0