Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manually define props type in vue3 with typescript

Tags:

vue.js

vuejs3

I can do

defineComponent({
  props: {
    name: { type: String as PropType<string> }
  }
})

to tell vue3 that my props type is { name: string }, but if I have several component have the same props type, how can I share the defination?

If I define props in :

const props = {
  name: String as PropType<string>
}

then use it like this:

defineComponent({
  props: props,
})

It won't work, the type I got in setup function of props is not right.

like image 452
jokcylou Avatar asked Aug 10 '20 06:08

jokcylou


People also ask

Does vue3 support TypeScript?

TypeScript also improves developer ergonomics via type-based auto-completion in IDEs. Vue is written in TypeScript itself and provides first-class TypeScript support. All official Vue packages come with bundled type declarations that should work out-of-the-box.

Can you use TypeScript with Vue?

Now that Vue. js officially supports TypeScript, it's possible to create TypeScript projects from scratch using Vue CLI. However, you still need some third-party packages with custom decorators and features to create a true, complete TypeScript application.

How do I add TypeScript to an existing Vue project?

To let Typescript enters into your Vue components, 2 additions are mandatory in each component file: Adding the lang="ts" attribute to the script tag so that Typescript can recognized it.

What are props in Vue API?

Props are a Vue runtime construct and can't be inferred from TypeScript type definitions (there are things like default values, validators, etc associated with props). You'll have to define them using the props option as the API expects.

How to create a component in Vue and typescript?

As covered in our previous article about Writing Single File Components in Typescript, there’s two ways of creating a “component” in Vue and Typescript: using vue-class-component or Vue.extend.

How to type component props in typescript?

As you see we can strongly type the component props using TypeScript typing system. The setup function is a new component method that acts as the entry point for using the Composition API with components. It gets invoked right after the initial props resolution when a component instance is created.

Should I use typescript or Vue for my App?

Now, you should be able to get your Vue app up and running in TypeScript with features like defineComponent, data, props, computed properties, methods, and watchers. Vue 3.0 includes better support for TypeScript out of the box, and the entire Vue code was rewritten in TypeScript to improve maintainability.


Video Answer


1 Answers

this answer is purely addition to @Xinchao's answer.

one way is to destructure common props like following:

// taken from @Xinchao's answer 
const commonProps = {
  name: { type: String as PropType<string> }
}

defineComponent({
  props:{
    ...commonProps,

    extra: { }

  }
})

another way is to write function which returns specific object like following

function getStringProp(required=false) {
    return {
       type: String as PropType<string>,
       required,
       default: '',
    };
}


defineComponent({
  props:{
    name: getStringProp(true),
    nickName: getStringProp(),
    extra: { }

  }
})

this case specifically come handy where prop is Array or Object; where we can cast the type like following

function getArrayProp<T>(required=false) {
    return {
       type: Array as PropType<T[]>,
       required,
       default: () => [],
    };
}


defineComponent({
  props:{
    options: getArrayProp<Options>(true),
    stringOptions: getArrayProp<string>(true),
  }
})
like image 88
ashwin bande Avatar answered Oct 18 '22 03:10

ashwin bande