Skip to content

"Object is of type 'unknown'" when using event handler on non-ts svelte component #396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dominic-simplan opened this issue Jul 31, 2020 · 4 comments
Labels
question A user question

Comments

@dominic-simplan
Copy link

dominic-simplan commented Jul 31, 2020

Describe the bug
After upgrading to the latest (101.7.0) VSCode plugin, I get an error when using an event handler on a svelte component, which is written in plain js.

Usage:

import { PlainJsComponent } from "otherLibrary";

<div>
    <PlainJsComponent on:someEvent={onSomeEvent}/>
</div>

Manual typings (which worked previously):
declare module 'otherLibrary';

Error message: Object is of type 'unknown'

If I remove the event handler, no error is display.
Don't know if this is a bug or if I just need to adjust my type definitions. If it is the latter, in which way do I need to adjust the type definitions?

Expected behavior
It should be possible to use a non-ts svelte component from a ts svelte component.

System (please complete the following information):

  • OS: Windows 10
  • IDE: VSCode 1.47.2
  • Plugin/Package: "Svelte for VSCode 101.7.0"
"svelte": "^3.24.0",
"svelte-check": "^0.1.55",
"svelte-loader": "^2.13.6",
"svelte-preprocess": "^4.0.8",
"@tsconfig/svelte": "^1.0.3",
"typescript": "^3.9.7",

tsconfig:

{
    "extends": "@tsconfig/svelte/tsconfig.json",
    "include": [
        "src/**/*",
        "src/node_modules"
    ],
    "exclude": [
        "node_modules/*",
        "dist/*"
    ],
    "compilerOptions": {
        "strict": true,
        "noImplicitAny": true,
        "resolveJsonModule": true,
        "sourceMap": true,
        "importsNotUsedAsValues": "remove"
    }
}

Additional context
Add any other context about the problem here.

@dominic-simplan dominic-simplan added the bug Something isn't working label Jul 31, 2020
@dominic-simplan dominic-simplan changed the title "Object is of type 'unknown'" when importing non-ts svelte component "Object is of type 'unknown'" when using event handler on non-ts svelte component Jul 31, 2020
@jasonlyu123
Copy link
Member

Can you post your definition here? we change the component type check mechanism in #303.

@dominic-simplan
Copy link
Author

I just had this single line in my typings.d.ts:
declare module 'otherLibrary';

Just figured out I can do this to make the error disappear:

declare module 'otherLibrary' {
    export class PlainJsComponent {
        $$prop_def: any;
        $on: any;
    }
}

Would this be an appropriate solution in my case or is there a better way?

@dummdidumm
Copy link
Member

This solution is appropriate. We changed the internals so that a component event handler like your on:someEvent is converted to __sveltets_instanceOf(PlainJsComponent ).$on('someEvent', .... At this point the type of PlainJsComponent has to be known explicitely, which is why you need to define the class.

We should definitely provide some docs/guidance on this once we have decided on how to properly define typings.

@dominic-simplan
Copy link
Author

I see, thanks for the explanation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question A user question
Projects
None yet
Development

No branches or pull requests

3 participants