Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce With component #28

Closed
2 tasks done
xaviervia opened this issue Jan 27, 2022 · 0 comments · Fixed by #33
Closed
2 tasks done

Introduce With component #28

xaviervia opened this issue Jan 27, 2022 · 0 comments · Fixed by #33
Labels
enhancement New feature or request

Comments

@xaviervia
Copy link
Contributor

xaviervia commented Jan 27, 2022

Example:

type UserDataProps = {
  name: FacetProp<string>
  middlename?: FacetProp<string | undefined>
}

const UserData = ({ name, middlename }: UserDataProps) => {
  const nameFacet = useFacetWrap(name)
  const middlenameFacet = useFacetWrap(middlename)
  
  return (
    <div>
      <p>Name: <fast-text text={nameFacet} /></p>
      <With data={middlenameFacet}>
        {(middlename) => <p>Middlename: <fast-text text={middlename} /></p>}
      </With>
    </div>
  )
}

Why?

The current component that supports a similar use case, Mount, has the defect that TypeScript is not able to tell when the data used inside it is defined. It cannot use the when clause to refine the type. In particular, the following code will lead to an annoying type error (since you as a developer know the code is correct, but TypeScript doesn't)

type UserDataProps = {
  name: FacetProp<string>
  middlename?: FacetProp<string | undefined>
}

const UserData = ({ name, middlename }: UserDataProps) => {
  const nameFacet = useFacetWrap(name)
  const middlenameFacet = useFacetWrap(middlename)
  
  return (
    <div>
      <p>Name: <fast-text text={nameFacet} /></p>
      <Mount data={useFacetMap((middlename) => middlename != null, [],[middlenameFacet])}>
        <p>Middlename: <fast-text text={middlenameFacet} /></p> 
        {/* Since TypeScript cannot know that `middlenameFacet` now holds a `string` for sure and still thinks that
            it could be `string | undefined`, it will complain. The only way to fix this is to extract a new component
            or with a type assertion. Neither is good */}
      </With>
    </div>
  )
}

TODO

  • Implement
  • Document
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant