question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cross file template type checking - check that components are passed props with the correct types

See original GitHub issue

When one has a Vue file:

<script>
export default {
  name: 'Foo',
  props: { bar: String }
}
</script>

Such templates should show error:

<foo :bar="5"></foo>
<foo :bar="myNumber"></foo>

Currently we have:

interface ComponentData<T> {
  props: Record<string, any>;
  on: ComponentListeners<T>;
  directives: any[];
}

I’m wondering if we could do something like this. For each imported Vue component, generate its corresponding type definition such as:

interface CProp1 {
  msg: string;
  [other: string]: any;
}
interface ComponentData1<T> {
  props: CProp1;
  on: ComponentListeners<T>
  directives: any[]
}
export declare const __vlsComponentHelper1: {
  <T>(vm: T, tag: string, data: ComponentData1<Record<string, any>> & ThisType<T>, children: any[]): any
}

Then the virtual file with this content would complain:

__vlsComponentHelper1(this, 'foo', { props: { msg: 5 }, on: {}, directives: [] }, []) // Type 'number' is not assignable to string

It should be easy to translate the props interface:

  • If required: false is specified, translate to msg?: type. Otherwise translate to msg: type
  • type is inferred from constructor types. For example String -> string. If uninferrable, use any.

@ktsn What do you think?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

13reactions
ktsncommented, Jan 22, 2020

Yes, it will be possible! We already have rough idea to achieve it.

1reaction
mesqueebcommented, Jan 22, 2020

My preferred syntax would be close to the native PropType:

props: {
  userList: {
    type: Array as PropType<User[]>,
    default: () => []
  },
  category: {
    type: String as PropType<MY_ENUM>,
    required: true
  }
}

Having to define the prop types outside of the actual place you write the props, it would be less ideal in my opinion.

Also anything that stays closest to regular Vue syntax seems best for me. 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

Template type checking - Angular
Verifies that component/directive bindings are assignable to their @Input() s; Obeys TypeScript's strictNullChecks flag when validating the preceding mode ...
Read more >
Typing Svelte Component Props/Events/Slots [Feedback ...
Also: How do we make sure the types match the component ... but I think this is indendent of the type checking on...
Read more >
How to Pass React Props Safely with Prop Type Validation
The type checking will come in handy here. If we pass in something that fails the type check, warning will be triggered, allowing...
Read more >
check that components are passed props with the correct types #1596
Cross file template type checking - check that components are passed props with the correct types #1596. octref posted onGitHub. When one has...
Read more >
Vue Prop Type Validation | Pine Wu's Blog - matsu
A Vue project, even written in TypeScript, still contains template code that is not covered by the type system. Sure you can write...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found