添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
酒量小的太阳  ·  Thymeleaf ...·  2 月前    · 
力能扛鼎的剪刀  ·  PHP - 她和她的猫·  11 月前    · 
可爱的乌冬面  ·  Life Changing Gym & ...·  1 年前    · 

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 Argument of type 'symbol' is not assignable to parameter of type 'string | unique symbol' #6817 Argument of type 'symbol' is not assignable to parameter of type 'string | unique symbol' #6817 mitar opened this issue Oct 4, 2022 · 6 comments

3.2.40

Link to minimal reproduction

https://stackblitz.com/edit/vitejs-vite-fpdjeq?file=package.json%2Csrc%2Fcomponents%2FHelloWorld.vue%2Cindex.html&terminal=dev

Steps to reproduce

  • Run reproduction.
  • Run vue-tsc (or as configured in reproduction, use vite-plugin-checker)
  • What is expected?

    Type checking succeeds.

    What is actually happening?

    Type checking fails with:

    Argument of type 'symbol' is not assignable to parameter of type 'string | unique symbol'.
    

    I am not sure why type of NONE changes inside the template.

    System Info

    This is a type checking issue and not related to a particular browser.

      System:
        OS: Linux 5.0 undefined
        CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
        Memory: 0 Bytes / 0 Bytes
        Shell: 1.0 - /bin/jsh
      Binaries:
        Node: 16.14.2 - /usr/local/bin/node
        Yarn: 1.22.10 - /usr/local/bin/yarn
        npm: 7.17.0 - /usr/local/bin/npm
      npmPackages:
        vue: ^3.2.40 => 3.2.40 
              

    Workaround is to create two helper functions:

    function onNoneChange(event: Event) {
      onChange(event, NONE)
    function stateHasNONE(): boolean {
      return props.state.includes(NONE)
    

    This should not be necessary.

    Using NONE instead of this.NONE can resolve it temporarily.
    Maybe you can try this as an interim solution.

    // HelloWorld.tsx
    import { defineComponent, PropType } from "vue";
    const NONE: unique symbol = Symbol('NONE')
    type State = (string | typeof NONE)[];
    export default defineComponent({
      props: {
        state: {
          type: Array as PropType<State>,
          required: false
      emits: {
        'update:state': (state: State) => true
      setup() {
        const NONE_ERR: unique symbol = Symbol('NONE')
        const onChange = (e: Event, val: typeof NONE) => {
          console.log(val)
        return {
          NONE_ERR,
          onChange,
      render() {
        type aaa = typeof this.NONE_ERR // symbol
        type bbb = typeof NONE // unique symbol
        return <input type="text" onClick={(e) => this.onChange(e, NONE)} />
    

    I think the reason is typeof can't infer the key of obejct is unique symbol .

    const thisObj = {
          NONE_ERR,
          onChange,
     type RawBindings = typeof thisObj
     //   NONE_ERR: symbol;
     //   onChange: (e: Event, val: typeof NONE) => void;
              

    @posva this is expected behavior because in template NONE access via component context, and NONE type is change from unique symbol to symbol in setup return. It can reproduce with:

    import { defineComponent } from 'vue';
    const NONE: unique symbol = Symbol('none');
    const Comp = defineComponent({
      setup() {
        return { NONE };
    });
    onChange((new Comp()).NONE)
    function onChange(id: typeof NONE) { }

    A hack way is indirect expose NONE type to template:

    <script lang="ts" setup>
    const NONE: unique symbol = Symbol('none');
    type NONE_Type = typeof NONE;
    </script>
    <template>
      <input type="checkbox" @change="onChange($event, NONE as NONE_Type)" />
    </template>