Cross file template type checking - check that components are passed props with the correct types
See original GitHub issueWhen 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 tomsg?: type
. Otherwise translate tomsg: type
type
is inferred from constructor types. For exampleString -> string
. If uninferrable, useany
.
@ktsn What do you think?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes, it will be possible! We already have rough idea to achieve it.
My preferred syntax would be close to the native
PropType
: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. 😉