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
This type hint is wrong in that defaultHandler is marked optional. With this definition, when writing vite.config.ts with typescript strict flag enabled, the onwarn handler must handle the case of undefined being passed as defaultHandler argument.
constoptions={//...onwarn: (warning,defaultHandler)=>{defaultHandler(warning);// ERROR: `defaultHandler` is possibly 'undefined'.}}
However, the defaultHandler is always passed to the onwarn handler.
The typescript error is because (1) (Warning, ((Warning) => void) | undefined) => void is a subtype of (2) (Warning, (Warning => void)) => void, not the other way around. So you can't pass (2) into a place that accepts (1).
Describe the bug
Current type hint for
onwarn
is as follows:This type hint is wrong in that
defaultHandler
is marked optional. With this definition, when writingvite.config.ts
with typescriptstrict
flag enabled, theonwarn
handler must handle the case ofundefined
being passed asdefaultHandler
argument.However, the
defaultHandler
is always passed to theonwarn
handler.vite-plugin-svelte/packages/vite-plugin-svelte/src/utils/log.js
Line 153 in acb1f88
The typescript error is because (1)
(Warning, ((Warning) => void) | undefined) => void
is a subtype of (2)(Warning, (Warning => void)) => void
, not the other way around. So you can't pass (2) into a place that accepts (1).Therefore, the correct type should be to remove the optional mark
?
fromdefaultHandler
. Such definition still accepts anonwarn
function withoutdefaultHandler
parameter(warning) => void
.c.f. https://www.typescriptlang.org/docs/handbook/type-compatibility.html#comparing-two-functions
tested here: https://stackblitz.com/edit/vitejs-vite-86jcat?file=typescript.ts
Note that the bug only occurs when the typescript
strictFunctionTypes
flag, orstrict
flag is enabled. Without it, the type system is relaxed so that a supertype function type can go into a place that accepts subtype function type. (https://www.typescriptlang.org/docs/handbook/type-compatibility.html#function-parameter-bivariance)Reproduction URL
https://stackblitz.com/edit/vitejs-vite-86jcat?file=vite.config.ts
Reproduction
tsc --noEmit
in the terminal to typecheck using tscIt throws an error that
Cannot invoke an object which is possibly 'undefined'.
for the linedefaultHandler(warning)
invite.config.ts
.Logs
No response
System Info
The text was updated successfully, but these errors were encountered: