Subscribe on changes!

TypeScript - binding.arg of custom directive must be string or undefined only

avatar
Dec 20th 2020

Version

3.0.4

Reproduction link

https://codesandbox.io/s/thirsty-elgamal-5u8qe?file=/src/App.vue

Steps to reproduce

Use custom directive to change el.classList on binding.arg change (as dynamic directive argument). Error: "Argument of type 'string | undefined' is not assignable to parameter of type 'boolean | undefined'."

image

What is expected?

Use boolean value as dynamic directive argument

What is actually happening?

Type error

avatar
Dec 20th 2020

The argument is what goes after the :, v-mydirective:foo foo is the argument. The typings are correct.

Remember to use the forum or the Discord chat to ask questions! Also don't open issues with invalid or incomplete reproductions (https://new-issue.vuejs.org/?repo=vuejs/vue#why-repro)

avatar
Dec 20th 2020

@posva I'm very sorry for the uncompleted reproduction (saving error, fixed now). But I need to ensure. The case looks like a good candidate for the enhancement.

According with DirectiveBinding arg can't be boolean (packages/runtime-core/src/directives.ts).

export interface DirectiveBinding<V = any> {
  instance: ComponentPublicInstance | null
  value: V
  oldValue: V | null
  arg?: string
  modifiers: DirectiveModifiers
  dir: ObjectDirective<any, V>
}

Is it correct and expecting behavior? I think (see the reproduction) boolean as arg can be useful and works without using the DirectiveBinding interface.

avatar
Dec 20th 2020

The existing type is correct

avatar
Dec 20th 2020

Ok, thanks for the confirmation