Skip to content

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

Luke Duncalfe requested to merge github/fork/zcf0508/main into main

Created by: zcf0508

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.

Merge request reports