mpellegrini / fullstack-typescript-monorepo-starter

1 stars 1 forks source link

Allow explicit undefined for svelte (4) files. #160

Open pcollinsonline opened 3 days ago

pcollinsonline commented 3 days ago

The following component triggers theunicorn/no-useless-undefined error:

<script lang="ts">
  import type { HTMLAttributes } from 'svelte/elements'

  type $$Props = HTMLAttributes<HTMLDivElement> & {
    message?: string
  }

  export let message: $$Props['message'] = undefined
</script>

<div {...$$restProps}>{message ?? 'Hello World'}</div>

Removing the explicit undefined value of the message property satisfied the linter, but svelte check will fail with the following error:

Error: Argument of type '$$Props' is not assignable to parameter of type '{ message: string | undefined; }'.
  Property 'message' is optional in type 'HTMLAttributes<HTMLDivElement> & { message?: string | undefined; }' but required in type '{ message: string | undefined; }'. (ts)

  type $$Props = HTMLAttributes<HTMLDivElement> & {
    message?: string

I added the following as the last entry in the defineFlatConfig array in .../profile/svelte.js to disable the rule for .svelte files.

  // adding this for Svelte 4 where $$Props is being used
  // issue is that $$Props will report properties as being required
  // when an optional property is not explicitly set to undefined
  // (it will fail svelte check unless explicitly set to undefined)
  // ie:
  // type $$Props = { foo?: string }
  // export let foo: $$Props['foo']
  {
    files: ['**/*.svelte'],
    rules: {
      'unicorn/no-useless-undefined': 'off',
    },
  },