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.

emitDts fails for interfaces and classes defined in script tag

See original GitHub issue

Describe 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 types, 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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
dummdidummcommented, Sep 12, 2021

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.

1reaction
dummdidummcommented, Aug 16, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Relationship between a TypeScript class and an interface with ...
I'm having a bit of trouble locating any clear documentation or explanation for the seemingly-special relationship between a TypeScript class ...
Read more >
Handbook - Interfaces - TypeScript
In TypeScript, interfaces fill the role of naming these types, and are a powerful way of defining contracts within your code as well...
Read more >
Understanding and using interfaces in TypeScript
TypeScript has inbuilt support for interfaces. An interface defines the specifications of an entity. It lays out the contract that states ...
Read more >
TypeScript Class Attributes Bug #6900 - sveltejs/svelte - GitHub
When defining a class with attributes within a <script context="module" lang="ts"> <script lang="ts"> tag, it doesn't work.
Read more >
A Simple Guide to Typescript Interfaces: declaration & use cases
In TypeScript, an interface is an abstract type that tells the compiler which property names a given object can have. TypeScript creates ...
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