Skip to content

When runtime props are added, the generic type inference in defineComponent is lost. #14206

Closed as duplicate

Description

@jahnli

Vue version

lastest

Link to minimal reproduction

https://www.typescriptlang.org/play/?jsx=1#code/JYWwDg9gTgLgBAbzgEwKYDNgDtUGELgQ5YwA0cUGcAvnOlAXAOQBuArqkwNwBQPAxkQDO8fODgBeFBmx4CkYjAAUPOHAA8AFTioAHjFRZkQuACVUgqMnUio2AObkAhlgCeAPndKwDMEIBciHCqanAgQvaBtg5cwaFwADbAIoGaANoAurEhakQAkgYguEn8ANYA-IFKwIWp5NhouoFYbCAARqhQAJSS7nAsEMDIcTQ9En0IOXCCWCLTEGwkkhQYSgAMXXzxM3MAFi7ICXglpcvVtXCa9UZ6za0d3b2IU2qCizAAdCxOCRxwANRSACMvHiah8ED8H3yhWKwDK5Q+51QIGujS6oNC1C2oUoMDYUCwcCUYz6KjBGmQwBY7heoXUVJpCAhUPC9loCDeJC+Pw41HUAHpGbSKWp1GwEiLRWpmb4hB8kiIPiAnGAlMjUXAGnpScS6RT1EkRtL4qVUK4JAhtbpsSawUQ4WVLSSnvsjEdHaUNWidba7aERK4jpakPwCUJoIEmJBsAYoExyGAnMgqVhIswABxgXQJsJOKD2bBRgAs2dzbWgaCgUaB2bgEaSwwAxPxW0waH67VL-YgAFIAZQA8gA5D7RNPAdCuDVdTsmwVJbuirqz-UaAUSpdioXUpebNTUUghSbxFkBOBpJhs3NMRUwJgZELYjF8dRiMAjNkSABEgApXQDsRoA6EqABYR34jHelppEgWBOCAqBRgAgiUnDOPY8FwAATAArDQ5DQbB6FMAAQhAbS5k4aGBAAzGsNAZLQIQwiinrOjUKI+rouo7BARwKhA9hKEwnqoMg-i5mxmrWquAruFwQA

Steps to reproduce

import { defineComponent, ref } from 'vue';

const Comp = defineComponent(
  <T extends Record<string, any>>(props: { 
    msg: string; 
    list: T[]; 
    onItemClick?: (item: T, index: number) => void 
  }) => {
    const count = ref(0)

    const handleClick = (item: T, index: number) => {
      count.value += 1;
      props.onItemClick?.(item, index);
    }

    return () => (
      <div>
        <div>{props.msg} {count.value}</div>
        <ul>
          {props.list.map((item, index) => (
            <li 
              key={index}
              onClick={() => handleClick(item, index)}
              style={{ cursor: 'pointer', padding: '8px', margin: '4px', border: '1px solid #ccc' }}
            >
              {JSON.stringify(item)}
            </li>
          ))}
        </ul>
      </div>
    )
  },
  {
    props: ['msg', 'list']
  }
);

<Comp 
  msg="list" 
  list={[{ name: 'Alice', age: 25 }, { name: 'Bob', age: 30 }]} 
  onItemClick={(item, index) => console.log('Clicked:', item, index)}
/>;

When the following was added,{ props: ['msg', 'list']} At this point, the type is defined as any.

What is expected?

fixed

What is actually happening?

type is defined as any

System Info

Any additional comments?

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions