svelte-preprocess icon indicating copy to clipboard operation
svelte-preprocess copied to clipboard

Variables created with Pug/Svelte mixin +each are not recognized by Typescript

Open braindefender opened this issue 4 years ago • 6 comments

Problem

Variables created with Pug/Svelte mixin (i.e. +each('links as link')) are not recognized by Typescript analyzer and cause "Cannot find name 'link'. Did you mean 'links'?" error.

Imgur

Example code:

<template lang="pug">
ul.nav
  +each('links as link')
    li(class:active="{ link.active }")
      a(href!="{ link.link }") { link.title }
</template>

<script lang="ts">
interface Link {
  active: boolean 
  link: string
  title: string
}

let links: Link[] = [
  { active: false, link: '#', title: 'Test Link 1' }, 
  { active: false, link: '#', title: 'Test Link 2' }, 
  { active: false, link: '#', title: 'Test Link 3' }, 
]
</script>

Context

Editor: VS Code or NVim Used preprocessors: Typescript, Pug, Sass. svelte-preprocess version: 4.0.9

braindefender avatar Jul 31 '20 13:07 braindefender

If your build is running fine, then this is actually a problem with the language service not preprocessing the template before running type checks. See this issue for more info.

dummdidumm avatar Aug 01 '20 08:08 dummdidumm

@dummdidumm so is there a path forward right now, or is the status = WIP?

bdombro avatar Aug 19 '20 20:08 bdombro

There are two things that make this hard to implement. The first thing is that preprocessing can be asynchronous, but we need it to be synchronous - we somehow have to work around that. We don't have a good solution to this yet. Related: https://github.com/sveltejs/language-tools/issues/339 The other thing is that the results in case of an error will likely be suboptimal, because pug does not provide source maps, so the line numbers will be all wrong. There seems to be a npm package that does this, since it's not part of pug itself, it may be cumbersome to integrate.

dummdidumm avatar Aug 20 '20 08:08 dummdidumm

Has there been any update on this issue?

braebo avatar Oct 01 '20 01:10 braebo

Hi, it's been a year) The problem is not solved?🥲

fedorovvvv avatar Jun 07 '22 20:06 fedorovvvv

this issue still persists

magdasmircea avatar Apr 04 '24 07:04 magdasmircea