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

Type is lost when using loadable in generic #1018

Open
iRuj80 opened this issue Jan 17, 2025 · 0 comments
Open

Type is lost when using loadable in generic #1018

iRuj80 opened this issue Jan 17, 2025 · 0 comments

Comments

@iRuj80
Copy link

iRuj80 commented Jan 17, 2025

example:

List.tsx

type Props<T> = {
  dataSource: T[]
  rowKey?: ((item: T) => Key) | keyof T
  renderItem: (item: T, index: number) => ReactNode
}

const List = <T,>({
  dataSource = [],
  rowKey,
  ...otherProps
}: Props<T>) => {

  const items = dataSource.map((item: T, index: number) =>
    return (
      <li key={index}>
        {renderItem(item, index)}
      </li>
    )
  )

  return (
    <ul
      {...otherProps}
    >
      {items}
    </ul>
  )
}

index.ts

import loadable from '@loadable/component'

// option 1
const List = loadable(() => import(/* webpackChunkName: "List" */ './List')) - type T is lost -> becomes unknown

// but! if it doesn't get lost like that

// option 2
const List = <T>() => loadable <import('./List').ListProps<T>> (() => import(/* webpackChunkName: "List" */ './List'))

SomeoneComponent.tsx

import List from './index'

type TListItem = {
  iconSize: number;
  title: string;
  label: string;
}

const dataSource: TListItem[]  = Array.from({ length: 10 }).map((_, index) => ({
  iconSize: 10,
  title: `Item ${index}`,
  label: `Label ${index}`,
}))

// if i use the option 2
const ListTest = List<TListItem>()

<ListTest 
  dataSource={dataSource}
  rowKey="label"
  renderItem={(item: TListItem) => <ListItem {...item}/>}
/>

I attached a screenshot of what vscode writes if I use option 1

Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant