Skip to content

Commit 00d1b03

Browse files
authored
(fix) force boolean typing (#589)
In case of `export let bla = true/false` where the boolean typing is not given by user, prevent TS from thinking the type is `false`/`true` instead of `boolean`. #588
1 parent 6445a5f commit 00d1b03

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

packages/svelte2tsx/src/svelte2tsx/processInstanceScriptContent.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,15 @@ export function processInstanceScriptContent(
8383
const jsDocType = ts.getJSDocType(declaration);
8484
const type = tsType || jsDocType;
8585

86-
if (!ts.isIdentifier(identifier) || !type) {
86+
if (
87+
!ts.isIdentifier(identifier) ||
88+
(!type &&
89+
// Edge case: TS infers `export let bla = false` to type `false`.
90+
// prevent that by adding the any-wrap in this case, too.
91+
![ts.SyntaxKind.FalseKeyword, ts.SyntaxKind.TrueKeyword].includes(
92+
declaration.initializer?.kind,
93+
))
94+
) {
8795
return;
8896
}
8997
const name = identifier.getText();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
///<reference types="svelte" />
2+
<></>;function render() {
3+
4+
let bla = false;bla = __sveltets_any(bla);;
5+
let blubb = true;blubb = __sveltets_any(blubb);;
6+
let bla1: boolean = false;bla1 = __sveltets_any(bla1);;
7+
let blubb1: boolean = true;blubb1 = __sveltets_any(blubb1);;
8+
let a1 = true;a1 = __sveltets_any(a1);;let a2 = false;a2 = __sveltets_any(a2);;let b1: boolean = true;b1 = __sveltets_any(b1);;let b2: boolean = false;b2 = __sveltets_any(b2);;
9+
;
10+
() => (<></>);
11+
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: {} }}
12+
13+
export default class Input__SvelteComponent_ extends createSvelte2TsxComponent(__sveltets_partial(__sveltets_with_any_event(render))) {
14+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
export let bla = false;
3+
export let blubb = true;
4+
export let bla1: boolean = false;
5+
export let blubb1: boolean = true;
6+
export let a1 = true, a2 = false, b1: boolean = true, b2: boolean = false;
7+
</script>

0 commit comments

Comments
 (0)