Skip to content
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

Optional props: Type is not assignable to type 'IntrinsicAttributes & ...'. #347

Closed
dominic-simplan opened this issue Jul 24, 2020 · 7 comments
Labels
bug Something isn't working

Comments

@dominic-simplan
Copy link

dominic-simplan commented Jul 24, 2020

Describe the bug
I have a component like this:

<script lang="ts">
    export let width = "100%";
    export let height = "100px";

    export let responsive = false;
</script>

<div id="categorySelect" style="width: {width}; height: {height};" />

If I use this component from another component but do not define the width: <Abc height="100%" responsive={true} />, VSCode shows the following error:

Type '{ height: string; responsive: true; }' is not assignable to type 'IntrinsicAttributes & { width: string; height: string; responsive: boolean; }'.
  Property 'width' is missing in type '{ height: string; responsive: true; }' but required in type '{ width: string; height: string; responsive: boolean; }

Therefore I tried explicitly defining width as string | undefined:

<script lang="ts">
    export let width: string | undefined = "100%";
    export let height = "100px";

    export let responsive = false;
</script>

<div id="categorySelect" style="width: {width}; height: {height};" />

Now I get this error:
Type 'true' is not assignable to type 'false'
It complains because I am setting another value than the default value.

Most strangely, adding an event handler for an event which doesn't even exist fixes the errors:
<Abc height="100%" on:bla={e => {}} responsive={true} />

Expected behavior
It should be possible to skip properties with default value or use other values than the default value.

System (please complete the following information):

  • OS: Windows 10
  • IDE: VSCode 1.47.2
  • Plugin/Package: "Svelte for VSCode 101.3.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,
        "resolveJsonModule": true,
        "sourceMap": true
    }
}

Additional context
This is so strange, maybe something with my setup is wrong?
Related: #276

@dominic-simplan dominic-simplan added the bug Something isn't working label Jul 24, 2020
@dominic-simplan dominic-simplan changed the title Type is not assignable to type 'IntrinsicAttributes & ...'. Optional props: Type is not assignable to type 'IntrinsicAttributes & ...'. Jul 24, 2020
@jasonlyu123
Copy link
Member

Don't worry you just encounter a ton of problems at once.

  1. props without type annotation don't got annotate as optional in strict mode.
  2. let a = false has a type of false, not boolean. This is a special case, Within all the value types it is a special case we haven't considered.
  3. I guess it's because the event handler got transformed into a spread syntax with any type. ts just thought anything could happen so it turns off all the other check.

The event handler issue will be fixed once #303 is merged. It would have a different mechanism.

@dummdidumm
Copy link
Member

For 1.: I think it's this line which produces the bug. No prop has a type annotation -> code returns there and the as {..} typing will be omitted, which would contain the logic to mark it as optional. We probably should only return there if it's a js file which cannot have type annotations.

@jasonlyu123
Copy link
Member

Also this one I think

return `${identifier}: typeof ${key}`;

@jasonlyu123
Copy link
Member

What about this one

declare function __sveltets_partial<T>(a: T): Partial<T>
declare function __sveltets_any(dummy: any): any;

function render() {
    let b = false;
    let c = false;
    let d: 1 | 123 = 123;c = __sveltets_any(c);;

    return { b, ...__sveltets_partial({ c: c }), ...__sveltets_partial({ d: d }) }
}

https://www.typescriptlang.org/play/?strictNullChecks=false&ssl=10&ssc=2&pln=1&pc=1#code/CYUwxgNghgTiAEAzArgOzAFwJYHtXwH0CBnANxAgxA2IIAdZsoIAeAFQD4AKKALnjYBKfgAVGWZuw4AoUJFgIU6bHkIlylarSioAnl2DIAtkd38du4fAsBuadKWZc+OKlAwug+AG9p8f-AQ1PAARvAAvEjMxCB2AYHBYBFREDFxAUEY8MD8AIzwAD7wuQBMAMzJpWU2SZFEZBRUNAQWXGCCNnZ+AXAYyDD43qEANPAAdBP1Gk20DDBMEFxDYPxJAL6CoxNjU41a9OLMS9n8wPAb59JrQA

@jasonlyu123
Copy link
Member

jasonlyu123 commented Jul 25, 2020

About the event handler seems like every spread syntax on tag in svelte2tsx would cause the same problem that's nearly every directive with ':'. This is way more problem than I thought.

dummdidumm pushed a commit to dummdidumm/language-tools that referenced this issue Jul 27, 2020
Before this, `export let a = ''` would not be marked as optional because it had no type.
sveltejs#347

Also fixes a bug where file type was incorrectly inferred as jsx which occurs when user uses the default languages feature of svelte-preprocess. Instead of computing the file type inside svelte2tsx, it is now passed as an option.
dummdidumm added a commit that referenced this issue Jul 28, 2020
Before this, `export let a = ''` would not be marked as optional because it had no type.
#347

Also fixes a bug where file type was incorrectly inferred as jsx which occurs when user uses the default languages feature of svelte-preprocess. Instead of computing the file type inside svelte2tsx, it is now passed as an option.
@dummdidumm
Copy link
Member

Quick update:
Of the three bugs only

  • let a = false has a type of false, not boolean

still exists, which can be worked around by doing let a: boolean = false

@dummdidumm
Copy link
Member

Closing in favor of #588

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants