emitDts fails for interfaces and classes defined in script tag
See original GitHub issueDescribe the bug
If an interface or class is defined in the script tag, and is indirectly exported, e.g. export let foo: BarInterface
, emitDts fails silently and outputs no respective file.
This is because the generated dts
mode tsx by svelte2tsx
is something like this:
import { SvelteComponentTyped } from "svelte"
function render() {
interface Hello {
world: string
}
let foo: Hello
;
return { props: {foo: foo} as {foo: Hello}, slots: {}, getters: {}, events: {} }}
const __propDef = __sveltets_1_with_any_event(render());
export type InputProps = typeof __propDef.props;
export type InputEvents = typeof __propDef.events;
export type InputSlots = typeof __propDef.slots;
export default class Input extends SvelteComponentTyped<InputProps, InputEvents, InputSlots> {
}
Note the interface is in the render()
function. Typescript will throw an error (e.g. Return type of exported function has or is using private name 'Hello'.
) as the interface is scoped but it’s being exported. See example typescript playground showing the error.
However, if you’re using normal type
s, e.g. type Foo = {}
, it’ll work. Example typescript playground.
It’s also important to note that this happens in declaration: true
for tsconfig only.
To Reproduce Steps to reproduce the behavior:
Clone https://github.com/bluwy/emit-dts-interface-repro, and follow steps in readme
Expected behavior
Generate types for Svelte components that have interfaces or classes in the script tag. Or specifically, svelte2tsx
should extract these types out of the render()
function for the dts-mode tsx output.
Screenshots n/a
System (please complete the following information):
- OS: Ubuntu 21.04
- IDE: VSCode
- Plugin/Package: svelte2tsx
Additional context It doesn’t seem easy to extract the types. Maybe a different dts mode output is needed?
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:5 (5 by maintainers)
Top GitHub Comments
This should work for interfaces now. I didn’t add a transformation for classes, but that’s “works as designed” for now, so I’m closing this.
Mhm this is bad. The proposed enhanced typings for props/events/slots relies on interfaces (
$$Props
/$$Slots
/$$Events
) to be defined within the instance script. A possible solution would be to traverse the top level TS AST of the instance script only and check for interfaces, and move them out of the render function, but only if there’s no interface with the same name in the module script.