Repository: sveltejs/language-tools
Pull request #1390 (2022-02-16 15:13:05)

(fix) regression with boolean literal props type inference
Revert part of the change with #1378 regarding this. Misread the condition and thought it can be removed. My bad


Patch:
diff --git a/packages/svelte2tsx/src/svelte2tsx/nodes/ExportedNames.ts b/packages/svelte2tsx/src/svelte2tsx/nodes/ExportedNames.ts
index 8c2e7ee7..7614f265 100644
--- a/packages/svelte2tsx/src/svelte2tsx/nodes/ExportedNames.ts
+++ b/packages/svelte2tsx/src/svelte2tsx/nodes/ExportedNames.ts
@@ -111,7 +111,15 @@ export class ExportedNames {
             const jsDocType = ts.getJSDocType(declaration);
             const type = tsType || jsDocType;
 
-            if (!ts.isIdentifier(identifier) || !type) {
+            if (
+                !ts.isIdentifier(identifier) ||
+                (!type &&
+                    // Edge case: TS infers `export let bla = false` to type `false`.
+                    // prevent that by adding the any-wrap in this case, too.
+                    ![ts.SyntaxKind.FalseKeyword, ts.SyntaxKind.TrueKeyword].includes(
+                        declaration.initializer?.kind
+                    ))
+            ) {
                 return;
             }
             const name = identifier.getText();


Test patch:
diff --git a/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/boolean-literal-props/Component.svelte b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/boolean-literal-props/Component.svelte
new file mode 100644
index 00000000..2b3dd730
--- /dev/null
+++ b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/boolean-literal-props/Component.svelte
@@ -0,0 +1,3 @@
+<script lang="ts">
+    export let flag = false;
+</script>
diff --git a/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/boolean-literal-props/expected.json b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/boolean-literal-props/expected.json
new file mode 100644
index 00000000..fe51488c
--- /dev/null
+++ b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/boolean-literal-props/expected.json
@@ -0,0 +1,1 @@
+[]
diff --git a/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/boolean-literal-props/expectedv2.json b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/boolean-literal-props/expectedv2.json
new file mode 100644
index 00000000..fe51488c
--- /dev/null
+++ b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/boolean-literal-props/expectedv2.json
@@ -0,0 +1,1 @@
+[]
diff --git a/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/boolean-literal-props/input.svelte b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/boolean-literal-props/input.svelte
new file mode 100644
index 00000000..af715571
--- /dev/null
+++ b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/boolean-literal-props/input.svelte
@@ -0,0 +1,5 @@
+<script lang="ts">
+    import Component from './Component.svelte'
+</script>
+
+<Component flag />
diff --git a/packages/svelte2tsx/test/svelte2tsx/samples/ts-export-boolean/expected.tsx b/packages/svelte2tsx/test/svelte2tsx/samples/ts-export-boolean/expected.tsx
index 1dcc2059..7d1bf98e 100644
--- a/packages/svelte2tsx/test/svelte2tsx/samples/ts-export-boolean/expected.tsx
+++ b/packages/svelte2tsx/test/svelte2tsx/samples/ts-export-boolean/expected.tsx
@@ -1,11 +1,11 @@
 ///<reference types="svelte" />
 <></>;function render() {
 
-     let bla = false;
-     let blubb = true;
+     let bla = false/*Ωignore_startΩ*/;bla = __sveltets_1_any(bla);/*Ωignore_endΩ*/;
+     let blubb = true/*Ωignore_startΩ*/;blubb = __sveltets_1_any(blubb);/*Ωignore_endΩ*/;
      let bla1: boolean = false/*Ωignore_startΩ*/;bla1 = __sveltets_1_any(bla1);/*Ωignore_endΩ*/;
      let blubb1: boolean = true/*Ωignore_startΩ*/;blubb1 = __sveltets_1_any(blubb1);/*Ωignore_endΩ*/;
-     let a1 = true;let  a2 = false;let  b1: boolean = true/*Ωignore_startΩ*/;b1 = __sveltets_1_any(b1);/*Ωignore_endΩ*/;let  b2: boolean = false/*Ωignore_startΩ*/;b2 = __sveltets_1_any(b2);/*Ωignore_endΩ*/;
+     let a1 = true/*Ωignore_startΩ*/;a1 = __sveltets_1_any(a1);/*Ωignore_endΩ*/;let  a2 = false/*Ωignore_startΩ*/;a2 = __sveltets_1_any(a2);/*Ωignore_endΩ*/;let  b1: boolean = true/*Ωignore_startΩ*/;b1 = __sveltets_1_any(b1);/*Ωignore_endΩ*/;let  b2: boolean = false/*Ωignore_startΩ*/;b2 = __sveltets_1_any(b2);/*Ωignore_endΩ*/;
 ;
 () => (<></>);
 return { props: {bla: bla , blubb: blubb , bla1: bla1 , blubb1: blubb1 , a1: a1 , a2: a2 , b1: b1 , b2: b2} as {bla?: typeof bla, blubb?: typeof blubb, bla1?: boolean, blubb1?: boolean, a1?: typeof a1, a2?: typeof a2, b1?: boolean, b2?: boolean}, slots: {}, getters: {}, events: {} }}
diff --git a/packages/svelte2tsx/test/svelte2tsx/samples/ts-export-boolean/expectedv2.ts b/packages/svelte2tsx/test/svelte2tsx/samples/ts-export-boolean/expectedv2.ts
index 69a26635..e2cfdca2 100644
--- a/packages/svelte2tsx/test/svelte2tsx/samples/ts-export-boolean/expectedv2.ts
+++ b/packages/svelte2tsx/test/svelte2tsx/samples/ts-export-boolean/expectedv2.ts
@@ -1,11 +1,11 @@
 ///<reference types="svelte" />
 ;function render() {
 
-     let bla = false;
-     let blubb = true;
+     let bla = false/*Ωignore_startΩ*/;bla = __sveltets_1_any(bla);/*Ωignore_endΩ*/;
+     let blubb = true/*Ωignore_startΩ*/;blubb = __sveltets_1_any(blubb);/*Ωignore_endΩ*/;
      let bla1: boolean = false/*Ωignore_startΩ*/;bla1 = __sveltets_1_any(bla1);/*Ωignore_endΩ*/;
      let blubb1: boolean = true/*Ωignore_startΩ*/;blubb1 = __sveltets_1_any(blubb1);/*Ωignore_endΩ*/;
-     let a1 = true;let  a2 = false;let  b1: boolean = true/*Ωignore_startΩ*/;b1 = __sveltets_1_any(b1);/*Ωignore_endΩ*/;let  b2: boolean = false/*Ωignore_startΩ*/;b2 = __sveltets_1_any(b2);/*Ωignore_endΩ*/;
+     let a1 = true/*Ωignore_startΩ*/;a1 = __sveltets_1_any(a1);/*Ωignore_endΩ*/;let  a2 = false/*Ωignore_startΩ*/;a2 = __sveltets_1_any(a2);/*Ωignore_endΩ*/;let  b1: boolean = true/*Ωignore_startΩ*/;b1 = __sveltets_1_any(b1);/*Ωignore_endΩ*/;let  b2: boolean = false/*Ωignore_startΩ*/;b2 = __sveltets_1_any(b2);/*Ωignore_endΩ*/;
 ;
 async () => {};
 return { props: {bla: bla , blubb: blubb , bla1: bla1 , blubb1: blubb1 , a1: a1 , a2: a2 , b1: b1 , b2: b2} as {bla?: typeof bla, blubb?: typeof blubb, bla1?: boolean, blubb1?: boolean, a1?: typeof a1, a2?: typeof a2, b1?: boolean, b2?: boolean}, slots: {}, getters: {}, events: {} }}