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.
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. https://github.com/sveltejs/vite-plugin-svelte/blob/acb1f886c0c218fe2cd5fb70d2b607878c2c6791/packages/vite-plugin-svelte/src/utils/log.js#L153The 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.tsNote 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
1) Run
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