You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
unknown is a safer alternative to any. SWR defaults to any for data and error, error being the bigger problem as its type can't be inferred.
The problem with any is that I can do error.foo.bar() and get a runtime error. This is not the best practice for properly typed code. unknown forces you to do the required type checks (e.g. error instanceof Error) before working with the variable.
That's also why Typescript since 4.0 requires you to specify catch (error: any) or catch (error: unknown) instead of defaulting to any to allow users to opt into a more type-safe option. And since 4.4 there's also a useUnknownInCatchVariables option to simply default to unknown.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
unknown
is a safer alternative toany
. SWR defaults toany
fordata
anderror
,error
being the bigger problem as its type can't be inferred.The problem with
any
is that I can doerror.foo.bar()
and get a runtime error. This is not the best practice for properly typed code.unknown
forces you to do the required type checks (e.g.error instanceof Error
) before working with the variable.That's also why Typescript since 4.0 requires you to specify
catch (error: any)
orcatch (error: unknown)
instead of defaulting toany
to allow users to opt into a more type-safe option. And since 4.4 there's also auseUnknownInCatchVariables
option to simply default tounknown
.I feel like SWR lags behind these Typescript trends and causes issues with recommended eslint rules like this one https://typescript-eslint.io/rules/no-unsafe-assignment/
Can we replace
any
withunknown
in SWR types for 2.0.0?Beta Was this translation helpful? Give feedback.
All reactions