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

defineProps() compiler hint fails if wrapped in another function call, i.e. toRefs #5104

Closed
zhangenming opened this issue Dec 14, 2021 · 8 comments
Labels

Comments

@zhangenming
Copy link
Contributor

zhangenming commented Dec 14, 2021

Version

3.2.26

Reproduction link

sfc.vuejs.org/

Steps to reproduce

review comp.vue

What is expected?

work well

What is actually happening?

defineProps is not defined

@caozhong1996
Copy link
Contributor

Duplicate #4994, this is the expected behavior, but maybe people need a proper hint from the document.

@zhangenming
Copy link
Contributor Author

So why can't we improve him? Is there any other reason? thanks

@LinusBorg LinusBorg changed the title defineProps is not defined defineProps() compiler hint fails if wrapped in another function call, i.e. toRefs Dec 14, 2021
@caozhong1996
Copy link
Contributor

Only stand for my personal view:

<script setup>
import { computed, onUpdated, ref, toRefs } from 'vue'
 
const props = defineProps({
  value: Number,
})
const { value } = toRefs(props)

// If we can do this
// const { value } = toRefs(defineProps({
//   value: Number,
// }))

// How about this? Please note that, props still can be declared, it's so weird 
// if ( a > 1 ) {
//   const props = defineProps({
//     value: Number,
//   })
// }

</script>

Macro is magic, we have to be careful while using. We hope props should be declared in the outermost scope, equivalent to

defineComponent({
  props: {
    //....
  },
  setup () {
    //....
  }
})

this should not be allowed

defineComponent({
  setup () {
    if ( a > 1 ) {
      props: {
       //....
      },
    }
  }
})

@zhangenming
Copy link
Contributor Author

// If we can do this
// const { value } = toRefs(defineProps({
//   value: Number,
// }))

But this is a common way to use it, we want props be reactive
or introduce a sugar grammar , make props be reactive by default

@caozhong1996
Copy link
Contributor

Yep, I'm with you, meanwhile, we'd better not open Pandora's Box.

@yyx990803
Copy link
Member

defineProps must be a top-level call with the exception of withDefaults.

Also with reactivity transform, there is no need to use toRefs() on it.

@tjx666
Copy link

tjx666 commented Jun 16, 2022

defineProps must be a top-level call with the exception of withDefaults.

Also with reactivity transform, there is no need to use toRefs() on it.

@yyx990803

This is my use case:

<script setup lang="ts">
import { ref, onMounted, watch, toRefs } from 'vue';
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
import './userWorker';

type Editor = ReturnType<typeof monaco.editor.create>;
type SupportedLanguage = 'typescript' | 'javascript' | 'json';

interface EditorProps {
    source: string;
    language: SupportedLanguage;
}
// !: using toRefs(defineProps()) to destructure props
const { source, language } = toRefs(defineProps<EditorProps>());

const editorContainer = ref<HTMLDivElement | null>(null);
const editor = ref<Editor | null>(null);

onMounted(() => {
    editor.value = monaco.editor.create(editorContainer.value!, {
        value: source.value,
        language: language.value,
    });
});

@SSQLQIANBB
Copy link

SSQLQIANBB commented Jul 21, 2022

const { isEdit } = toRefs(
    withDefaults(
      defineProps<{
        isEdit?: boolean;
      }>(),
      { isEdit: true }
    )
  );

why withDefaults is not defined
but bottom is right

  const props = withDefaults(
    defineProps<{
      isEdit?: boolean;
    }>(),
    { isEdit: true }
  );
  const { isEdit } = toRefs(props);

@github-actions github-actions bot locked and limited conversation to collaborators Sep 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants