Typescript each indexed (#9567)

* failing test

* fix undefined index bug

* Update packages/svelte/src/compiler/phases/1-parse/read/context.js

Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>

---------

Co-authored-by: Rich Harris <rich.harris@vercel.com>
Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
pull/9542/head
Rich Harris 9 months ago committed by GitHub
parent 95c6b65f6f
commit 1c48d7cbdc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -106,7 +106,12 @@ function read_type_annotation(parser) {
const insert = '_ as ';
let a = parser.index - insert.length;
const template = ' '.repeat(a) + insert + parser.template.slice(parser.index);
const expression = parse_expression_at(template, parser.ts, a);
let expression = parse_expression_at(template, parser.ts, a);
// `array as item: string, index` becomes `string, index`, which is mistaken as a sequence expression - fix that
if (expression.type === 'SequenceExpression') {
expression = expression.expressions[0];
}
parser.index = /** @type {number} */ (expression.end);
return /** @type {any} */ (expression).typeAnnotation;

@ -0,0 +1,9 @@
import { test } from '../../test';
export default test({
html: `
<span>0: a</span>
<span>1: b</span>
<span>2: c</span>
`
});

@ -0,0 +1,7 @@
<script lang="ts">
let letters = ['a', 'b', 'c'];
</script>
{#each letters as letter: string, i}
<span>{i}: {letter}</span>
{/each}
Loading…
Cancel
Save