Typescript support and examples
See original GitHub issueDescribe the bug
Problems while creating the Options field for loadModule
To Reproduce
<template>
<component :is="computedComponent" />
</template>
<script lang="ts">
import { loadModule } from "vue3-sfc-loader/src/index"
import { defineComponent, defineAsyncComponent } from "vue"
const options = {
async getFile(url: string) {
const res = await fetch(url)
if ( !res.ok )
throw Object.assign(new Error(`${res.statusText} ${url}`), { res })
return await res.text()
},
addStyle(textContent: any) {
const style = Object.assign(document.createElement("style"), { textContent })
const ref = document.head.getElementsByTagName("style")[0] || null
document.head.insertBefore(style, ref)
},
}
export default defineComponent({
data() {
return {
currentComponent: "./DashBoard.vue",
}
},
props: {
url: {
type: String,
required: true
},
},
computed: {
computedComponent() {
const currentComponent: String = this.url // the trick is here
return defineAsyncComponent( () => loadModule(currentComponent, options) )
}
},
})
</script>
Expected behavior It should works but it gets:
ERROR in src/components/Page.vue:41:77
TS2345: Argument of type '{ getFile(url: string): Promise<string>; addStyle(textContent: any): void; }' is not assignable to parameter of type 'Options'.
Type '{ getFile(url: string): Promise<string>; addStyle(textContent: any): void; }' is missing the following properties from type 'Options': pathResolve, getResource
39 | computedComponent() {
40 | const currentComponent: String = this.url // the trick is here
> 41 | return defineAsyncComponent( () => loadModule(currentComponent, options) )
| ^^^^^^^
42 | }
43 | },
44 | })
Versions
- Compiling via typescript:
- vue3-sfc-loader: latest
Issue Analytics
- State:
- Created 2 years ago
- Comments:11 (3 by maintainers)
Top Results From Across the Web
TypeScript: JavaScript With Syntax For Types.
TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale. Try TypeScript Now. Online or...
Read more >Documentation - TypeScript for JavaScript Programmers
Defining Types To cover these cases, TypeScript supports an extension of the JavaScript language, which offers places for you to tell TypeScript what...
Read more >The starting point for learning TypeScript
Find TypeScript starter projects: from Angular to React or Node.js and CLIs.
Read more >Documentation - TypeScript Tooling in 5 minutes
A tutorial to understand how to create a small website with TypeScript.
Read more >TypeScript: Documentation - The Basics
An editor that supports TypeScript can deliver “quick fixes” to automatically fix errors, refactorings to easily re-organize code, and useful navigation ...
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
The main issue is that webpack doesn’t build a proper
d.ts
file. It should be a singled.ts
, referenced throughpackage.json
typings
field.We have to find a way to build a single
d.ts
file matching the dist bundle. Maybe the easier to achieve this is to manually writed.ts
file and implement it in TypeScript sources.Seems that you’re missing the moduleCache key on options object.