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

fix(types): fix props type error with js script #13063

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

zcf0508
Copy link

@zcf0508 zcf0508 commented Aug 4, 2023

What kind of change does this PR introduce? (check at least one)

  • Bugfix
  • Feature
  • Code style update
  • Refactor
  • Build-related changes
  • Other, please describe:

Does this PR introduce a breaking change? (check one)

  • Yes
  • No

If yes, please describe the impact and migration path for existing applications:

The PR fulfills these requirements:

If adding a new feature, the PR's description includes:

  • A convincing reason for adding this feature (to avoid wasting your time, it's best to open a suggestion issue first and wait for approval before working on it)

Other information:

When I write a sfc file with js, the type of props inference to any.

<script>
   //  ^ use js
import { defineComponent } from 'vue';
export default defineComponent({
  props: {
    a: Number
  },
  setup(props) {
    // ^ props is any
  }
})
</script>

The defineComponent function allows a Props type to be passed in.
All work fine with ts because if not actively passed in this type, the default props type is unkonwn.

But when I use js to write the code, the default type is any! I think it might be a typescript problem.

I think the problem that went wrong is here.

https://github.com/vuejs/vue/blob/main/types/v3-define-component.d.ts#L153
https://github.com/vuejs/vue/blob/main/types/common.d.ts#L15

options: HasDefined<Props> extends true
    //   ^ here
    ? { functional?: never } & ComponentOptionsWithProps<
        PropsOptions,
        RawBindings,
        D,
        C,
        M,
        Mixin,
        Extends,
        Emits,
        EmitsNames,
        Props
      >
    : { functional?: never } & ComponentOptionsWithProps<
        PropsOptions,
        RawBindings,
        D,
        C,
        M,
        Mixin,
        Extends,
        Emits,
        EmitsNames
      >

I think Props type should be not defined when it is any . And in that case, typescript would infer the actual type of the props.

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

Successfully merging this pull request may close these issues.

1 participant