Code Monkey home page Code Monkey logo

Comments (3)

hyunbinseo avatar hyunbinseo commented on August 22, 2024

I am not sure if this issue belongs in the language-tools repo.

I cannot get $props().children to be typed, and it was raised over there.

<script>
  // let children: unknown
  let { children } = $props();
</script>

<!-- 'children' is of type 'unknown'.js(18046) -->
{@render children()}

from svelte.

jasonlyu123 avatar jasonlyu123 commented on August 22, 2024

To type the prop you need to type the props object and not individual props.

https://svelte-5-preview.vercel.app/docs/runes#$props

The unknown part is generated by language-tools. But without that, it will still be inferred as any. Not sure if it makes sense to allow it since it isn't how typescript inferred the type for destructuring.

from svelte.

hyunbinseo avatar hyunbinseo commented on August 22, 2024

So I guess there is no special return type for $bindable? Is this good?

<script lang="ts">
  type Props = { b: boolean };
  let { b = $bindable<boolean>() }: Props = $props();
</script>

Wished this was documented in the $bindable preview documentation.


I know TypeScript object must be typed as a whole, but still:

  1. If default values are provided, manual typing might not be necessary:
<script lang="ts">
  let {
    // Only this prop needs typing,
    // but the whole Props should be typed.
    showModal = $bindable<boolean>(),
    closeWithBackdropClick = false,
    onclose = (() => {}) as () => unknown
  } = $props();
</script>
  1. props and the Props type can mismatch:
<!-- src/lib/Component.svelte -->
<script lang="ts">
  // Even-though the fallback value has been provided,
  let { b = $bindable(false) }: Props = $props();

  // Since it is not a optional key in the Props type:
  type Props = { b: boolean };
</script>
<!-- src/routes/+page.svelte -->
<script>
  import Component from '$lib/Component.svelte';
</script>

<!-- Property 'b' is missing in type '{}' but required in type 'Props'. -->
<Component></Component>

from svelte.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.