Skip to content

possible to provide a compile-time alternative to splitProps? #6

@Enteleform

Description

@Enteleform

Current Limitation

Using const to define a component prevents utilization of TypeScript's namespaces to encapsulate runtime entities, only types can be defined with the namespace.

import {component} from "babel-plugin-solid-undestructure"

export const Greeting = component(({name, greeting="Hello"}:Greeting.Props) => <h1>{greeting} {name}</h1>)

export namespace Greeting{
  export type Props = {
    name:      string
    greeting?: ("Hello" | "Howdy")
  }
  
  /* Error: Cannot redeclare block-scoped variable 'Greeting'.ts(2451) */
  //export function alert({name, greeting="Hello"}:Props){
  //  window.alert(`${greeting} ${name}`)
  //}
}

Proposed Solution

An undestructure compile-type function annotation that can be used within a function component would allow full utilization of TypeScript's namespaces.

This could also allow solid-undestructure's functionality to be utilized in non-component utility functions. I'm still not 100% clear on the use cases for this, but from @ryansolid's comment @ Discord, it seems that it could prevent reactivity from being broken in certain scenarios.

image

import {undestructure} from "babel-plugin-solid-undestructure"

export function Greeting(props:Greeting.Props){
  /* Preferable to `splitProps` in some cases,
  *  because it exposes variables directly into the scope
  *  rather than encapsulating them, and also doesn't require
  *  explicit definition of keys to be split. */
  const {name, greeting="Hello"} = undestructure(props)
  return <h1>{greeting} {name}</h1>
}

export namespace Greeting{
  export type Props = {
    name:      string
    greeting?: ("Hello" | "Howdy")
  }
  
  /* Works, no error. */
  export function alert({name, greeting="Hello"}:Props){
    window.alert(`${greeting} ${name}`)
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions