Skip to content

feat: typescript 7 content mapper - #3111

Merged
dummdidumm merged 58 commits into
sveltejs:masterfrom
jasonlyu123:ts7-content-mapper
Nov 4, 2026
Merged

dummdidumm merged 58 commits into
sveltejs:masterfrom
jasonlyu123:ts7-content-mapper

Conversation

@jasonlyu123

@jasonlyu123 jasonlyu123 commented Aug 22, 2026 •

Copy link
Copy Markdown
Member

#2733

The language server part currently requires changes in the TypeScript 7 VSCode extension that haven't been published yet. This can only be tested with a local TypeScript extension build.

This PR is focused on editor support. Svelte-check support will be a separate PR.

Known problems

  • inlay hints
    Duplicated entries. Opened an upstream issue and also reduced the places where it happens.
  • document highlight
    VS Code doesn't seem to allow multiple providers. Need to manually merge it when we have a middleware api.
  • SvelteKit auto-typing
  • Some Svelte 4 legacy features were unmappable and currently can't be filtered out.

PR Test instruction

(Will edit this after this change is published)

  • Install the TypeScript 7 extension and the TypeScript 7 nightly extension. tsdk config should also work but svelte-check --tsgo-experimental-api isn't compatiable with the latest nightly version.So you need to avoid workspace tsdk config.
  • Install content mapper and svelte2tsx using a relative path to a test Svelte project.
{
  "devDependencies": {
  	"@sveltejs/ts-content-mapper": "file:../language-tools/packages/typescript-content-mapper"
  },
  "pnpm": {
  	"overrides": {
  		"@sveltejs/load-config": "file:../language-tools/packages/load-config",
  		"svelte2tsx": "file:../language-tools/packages/svelte2tsx"
  	}
  }
  • Add this to the tsconfig.json of the test project
	"contentMappers": [
  	{
  		"package": "@sveltejs/ts-content-mapper",
  		"extensions": [".svelte"]
  	}
  ]
@changeset-bot

changeset-bot Bot commented Aug 22, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 1d0aeaa

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
svelte-language-server Patch
svelte2tsx Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@andrewbranch

Copy link
Copy Markdown

Exciting progress! Do you have a stack trace for the auto-import panic handy?

@jasonlyu123

Copy link
Copy Markdown
Member Author

Do you have a stack trace for the auto-import panic handy?

Debug failure. False expression: Token end is child end
runtime/debug.Stack()
	C:/Program Files/Go/src/runtime/debug/stack.go:26 +0x8e
github.com/microsoft/TypeScript/tsc/internal/lsp.(*Server).recover(0x25835d125508, 0x25836906e3f0)
	C:/projects/TypeScript/tsc/internal/lsp/server.go:1477 +0x4f
panic({0x7ff7b8bc3580?, 0x2583610277f0?})
	C:/Program Files/Go/src/runtime/panic.go:860 +0x14f
github.com/microsoft/TypeScript/tsc/internal/debug.Fail({0x258368c81bc0, 0x37})
	C:/projects/TypeScript/tsc/internal/debug/debug.go:14 +0x65
github.com/microsoft/TypeScript/tsc/internal/debug.assertSlow({0x258368d9cde8, 0x1, 0x1})
	C:/projects/TypeScript/tsc/internal/debug/debug.go:60 +0xa5
github.com/microsoft/TypeScript/tsc/internal/debug.Assert(0x0, {0x258368d9cde8, 0x1, 0x1})
	C:/projects/TypeScript/tsc/internal/debug/debug.go:49 +0x3b
github.com/microsoft/TypeScript/tsc/internal/format.(*formatSpanWorker).processChildNode(0x25835f7505a0, 0x258369118000, 0x258369115400, 0x0, 0x0, 0x25836205f6c8, 0xffffffffffffffff, 0x258369118000, 0x258369115400, 0x0, ...)
	C:/projects/TypeScript/tsc/internal/format/span.go:410 +0x957
github.com/microsoft/TypeScript/tsc/internal/format.(*formatSpanWorker).execute.func1(0x25836205f6c8)
	C:/projects/TypeScript/tsc/internal/format/span.go:236 +0x15a
github.com/microsoft/TypeScript/tsc/internal/ast.(*NodeVisitor).VisitNode(0x2583690fb620, 0x25836205f6c8)
	C:/projects/TypeScript/tsc/internal/ast/visitor.go:51 +0x87
github.com/microsoft/TypeScript/tsc/internal/ast.(*NodeVisitor).visitNode(0x2583690fb620, 0x25836205f6c8)
	C:/projects/TypeScript/tsc/internal/ast/visitor.go:198 +0x6a
github.com/microsoft/TypeScript/tsc/internal/ast.(*ImportDeclaration).VisitEachChild(0x258369118000, 0x2583690fb620)
	C:/projects/TypeScript/tsc/internal/ast/ast_generated.go:2564 +0xa5
github.com/microsoft/TypeScript/tsc/internal/ast.(*Node).VisitEachChild(0x258369118000, 0x2583690fb620)
	C:/projects/TypeScript/tsc/internal/ast/ast.go:208 +0x42
github.com/microsoft/TypeScript/tsc/internal/format.(*formatSpanWorker).executeProcessNodeVisitor(0x25835f7505a0, 0x258369118000, 0x258369115400, 0x0, 0x0)
	C:/projects/TypeScript/tsc/internal/format/span.go:537 +0x125
github.com/microsoft/TypeScript/tsc/internal/format.(*formatSpanWorker).processNode(0x25835f7505a0, 0x258369118000, 0x2583690f4388, 0x0, 0x0, 0x4, 0x4)
	C:/projects/TypeScript/tsc/internal/format/span.go:638 +0x156
github.com/microsoft/TypeScript/tsc/internal/format.(*formatSpanWorker).processChildNode(0x25835f7505a0, 0x2583690f4388, 0x258369115360, 0x0, 0x0, 0x258369118000, 0xffffffffffffffff, 0x2583690f4388, 0x258369115360, 0x0, ...)
	C:/projects/TypeScript/tsc/internal/format/span.go:428 +0xb05
github.com/microsoft/TypeScript/tsc/internal/format.(*formatSpanWorker).processChildNodes(0x25835f7505a0, 0x2583690f4388, 0x258369115360, 0x0, 0x0, 0x2583619c9140, 0x2583690f4388, 0x0, 0x258369115360)
	C:/projects/TypeScript/tsc/internal/format/span.go:501 +0x36c
github.com/microsoft/TypeScript/tsc/internal/format.(*formatSpanWorker).execute.func2(0x2583619c9140, 0x2583690fb620)
	C:/projects/TypeScript/tsc/internal/format/span.go:243 +0x10d
github.com/microsoft/TypeScript/tsc/internal/ast.(*NodeVisitor).visitNodes(0x2583690fb620, 0x2583619c9140)
	C:/projects/TypeScript/tsc/internal/ast/visitor.go:234 +0x4b
github.com/microsoft/TypeScript/tsc/internal/ast.(*NodeVisitor).visitTopLevelStatements(0x2583690fb620, 0x2583619c9140)
	C:/projects/TypeScript/tsc/internal/ast/visitor.go:257 +0x6a
github.com/microsoft/TypeScript/tsc/internal/ast.(*SourceFile).VisitEachChild(0x2583690f4388, 0x2583690fb620)
	C:/projects/TypeScript/tsc/internal/ast/ast.go:2798 +0x45
github.com/microsoft/TypeScript/tsc/internal/ast.(*Node).VisitEachChild(0x2583690f4388, 0x2583690fb620)
	C:/projects/TypeScript/tsc/internal/ast/ast.go:208 +0x42
github.com/microsoft/TypeScript/tsc/internal/format.(*formatSpanWorker).executeProcessNodeVisitor(0x25835f7505a0, 0x2583690f4388, 0x258369115360, 0x0, 0x0)
	C:/projects/TypeScript/tsc/internal/format/span.go:537 +0x125
github.com/microsoft/TypeScript/tsc/internal/format.(*formatSpanWorker).processNode(0x25835f7505a0, 0x2583690f4388, 0x2583690f4388, 0x0, 0x0, 0x4, 0x4)
	C:/projects/TypeScript/tsc/internal/format/span.go:638 +0x156
github.com/microsoft/TypeScript/tsc/internal/format.(*formatSpanWorker).execute(0x25835f7505a0, 0x25836437d130)
	C:/projects/TypeScript/tsc/internal/format/span.go:257 +0x593
github.com/microsoft/TypeScript/tsc/internal/format.newFormattingScanner({0x258364ff7800, 0x2f}, 0x0, 0x0, 0x2f, 0x25835f7505a0)
	C:/projects/TypeScript/tsc/internal/format/scanner.go:57 +0x189
github.com/microsoft/TypeScript/tsc/internal/format.FormatNodeGivenIndentation({0x7ff7b9033b98, 0x258368dfc1b0}, 0x2583690f4388, 0x2583690f4388, 0x0, 0x4, 0x4)
	C:/projects/TypeScript/tsc/internal/format/api.go:83 +0x15f
github.com/microsoft/TypeScript/tsc/internal/ls/change.(*Tracker).getFormattedTextOfNode(0x2583678a5b20, 0x2583650558f0, 0x25836888a708, 0x25836888a708, 0x1f6, {{0x0, 0x0}, {0x7ff7b901e6e8, 0x1}, 0x0, ...})
	C:/projects/TypeScript/tsc/internal/ls/change/trackerimpl.go:140 +0x4b3
github.com/microsoft/TypeScript/tsc/internal/ls/change.(*Tracker).computeNewText.func1(0x2583650558f0)
	C:/projects/TypeScript/tsc/internal/ls/change/trackerimpl.go:86 +0x112
github.com/microsoft/TypeScript/tsc/internal/ls/change.(*Tracker).computeNewText(0x2583678a5b20, 0x2583690f3400, 0x25836888a708, 0x25836888a708)
	C:/projects/TypeScript/tsc/internal/ls/change/trackerimpl.go:98 +0x2d7
github.com/microsoft/TypeScript/tsc/internal/ls/change.(*Tracker).getTextChangesFromChanges.func2(0x2583690f3400)
	C:/projects/TypeScript/tsc/internal/ls/change/trackerimpl.go:43 +0x4b
github.com/microsoft/TypeScript/tsc/internal/core.MapNonNil[...]({0x2583690b53e8, 0x1, 0x1}, 0x258368d9e920)
	C:/projects/TypeScript/tsc/internal/core/core.go:120 +0xd9
github.com/microsoft/TypeScript/tsc/internal/ls/change.(*Tracker).getTextChangesFromChanges(0x2583678a5b20)
	C:/projects/TypeScript/tsc/internal/ls/change/trackerimpl.go:40 +0x2de
github.com/microsoft/TypeScript/tsc/internal/ls/change.(*Tracker).GetChanges(0x2583678a5b20)
	C:/projects/TypeScript/tsc/internal/ls/change/tracker.go:137 +0x65
github.com/microsoft/TypeScript/tsc/internal/ls/autoimport.fileEdits(0x2583678a5b20, 0x25836888a708)
	C:/projects/TypeScript/tsc/internal/ls/autoimport/fix.go:134 +0x66
github.com/microsoft/TypeScript/tsc/internal/ls/autoimport.(*Fix).Edits(_, {_, _}, _, _, {{0x0, 0x4, 0x4, {0x7ff7b901e6e8, 0x1}, ...}, ...}, ...)
	C:/projects/TypeScript/tsc/internal/ls/autoimport/fix.go:109 +0xe66
github.com/microsoft/TypeScript/tsc/internal/ls.(*LanguageService).filterContentMappedAutoImports(0x258361516b40, {0x7ff7b9033b98, 0x258367fdc960}, 0x25835f17fb88, 0x25836888a708, 0x258368dfc150)
	C:/projects/TypeScript/tsc/internal/ls/completions.go:89 +0x44f
github.com/microsoft/TypeScript/tsc/internal/ls.(*LanguageService).ProvideCompletion(0x258361516b40, {0x7ff7b9033b98, 0x258367fdc960}, {0x258361234000, 0x44}, {0x8, 0x2}, 0x258363ef35a0)
	C:/projects/TypeScript/tsc/internal/ls/completions.go:70 +0x51c
github.com/microsoft/TypeScript/tsc/internal/lsp.(*Server).handleCompletion(0x25835d125508, {0x7ff7b9033b98, 0x258367fdc870}, 0x258361516b40, 0x258367fdc8a0)
	C:/projects/TypeScript/tsc/internal/lsp/server.go:2112 +0xe5
github.com/microsoft/TypeScript/tsc/internal/lsp.registerLanguageServiceWithAutoImportsRequestHandler[...].func1.1.1()
	C:/projects/TypeScript/tsc/internal/lsp/server.go:1374 +0x174
github.com/microsoft/TypeScript/tsc/internal/project.(*Session).WithLanguageServiceAndSnapshot.func1()
	C:/projects/TypeScript/tsc/internal/project/session.go:1336 +0xb1
github.com/microsoft/TypeScript/tsc/internal/lsp.(*Server).handleRequestOrNotification.func1()
	C:/projects/TypeScript/tsc/internal/lsp/server.go:1167 +0x75
github.com/microsoft/TypeScript/tsc/internal/lsp.(*Server).dispatchLoop.func3()
	C:/projects/TypeScript/tsc/internal/lsp/server.go:1014 +0x33
created by github.com/microsoft/TypeScript/tsc/internal/lsp.(*Server).dispatchLoop in goroutine 10
	C:/projects/TypeScript/tsc/internal/lsp/server.go:1013 +0x81f

Weirdly, in getFormattedTextOfNode, the text returned in t.getNonformattedText is a broken import statement.

import { SvelteComponentTyped } from te-tsgo-t;

And if I track down the module specifier printing in that function, in the getLiteralTextOfNode function here, p.emitContext.textSource is nil. It sounds like the emitContext might not be the same one when the node is created?

Btw, should I open an issue to track this? There is also a problem with duplicated inlay hints that seems to happen when a node is split into multiple spans. Should I open two separate issues?

@andrewbranch

Copy link
Copy Markdown

Two issues would be great, if you don't mind!

@jasonlyu123

Copy link
Copy Markdown
Member Author

A few content mapper tasks after this PR is merged:

  • Update svelte-check --tsgo-experimental-api to use TypeScript 7.1 stable api.
    This blocks all the other tasks because of API changes.

LSP

  • Add content-mapper support for svelte-check --tsgo-experimental-api
  • Port component-references-related features for TypeScript 7
  • Add LSP middlewares
  • Diagnotics/notification message for missing content-mapper config

Drop svelte-check's dependency on TypeScript 6

  • Update svelte2tsx to use TypeScript 7 AST api
  • Remove top-level imports of TypeScript 6 in svelte-check and language server.
@xyrolle

xyrolle commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

I tested the content mapper from d3766bc on a small fixture with Svelte 5.57.2, TypeScript 6.0.3, and @typescript/native aliased to typescript@7.1.0-beta (Node 24.16.0, macOS arm64).

With the mapper configured in contentMappers, I ran:

node node_modules/@typescript/native/bin/tsc -p tsconfig.cm.json --noEmit --pretty false --runExternalCode

A .ts file importing an exported snippet from <script module> passes; without the mapper it reports TS2614, as in #3083.

After adding a string-to-number assignment error, the mapper reports TS2322 at Children1Col.svelte:3:11, matching svelte-check 4.7.7.

These are CLI fixture checks; I haven't tested editor integration.

@xyrolle

xyrolle commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

One additional CLI repro with d3766bc, using the same setup as above: Svelte 5.57.2, TypeScript 6.0.3, and @typescript/native aliased to typescript@7.1.0-beta.

src/Gen.svelte:

<script lang="ts" generics="const T extends readonly string[]">
    let { items }: { items: T } = $props();
</script>

<p>{items.length}</p>

With the mapper configured in contentMappers, running:

node node_modules/@typescript/native/bin/tsc -p tsconfig.cm.json --noEmit --pretty false --runExternalCode

reports:

src/Gen.svelte(28,46): error TS1277: 'const' modifier can only appear on a type parameter of a function, method or class

The diagnostic notes that this is a virtual-code location with no corresponding source location. The generated type alias keeps const:

type Gen__SvelteComponent_<const T extends readonly string[]> = InstanceType<typeof Gen__SvelteComponent_<T>>;

TypeScript doesn't allow const on a type alias's type parameters (6.0.3 and 7.1.0-beta both give TS1277 for type A<const T extends readonly string[]> = T, and nothing without const). svelte-check 4.7.7 reports no errors for the original component in default and --tsgo modes.

@xyrolle

xyrolle commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Another CLI finding with d3766bc: with the mapper, TypeScript type-checks the .svelte sources a library ships next to their .svelte.d.ts, instead of using the declarations. On our app that was 124 errors inside layerchart 2.5.1 and bits-ui 2.19.5, which the default mode doesn't report.

Minimal repro, with the mapper from d3766bc installed as in the PR description, Svelte 5.57.2, TypeScript 6.0.3 and @typescript/native 7.1.0-beta (also 7.1.0-dev.20261022.1), Node 24.16.0, macOS arm64:

tsconfig.json:

{
  "compilerOptions": { "target": "ESNext", "module": "ESNext", "moduleResolution": "bundler", "strict": true, "noEmit": true, "skipLibCheck": true, "types": ["svelte"] },
  "include": ["src/**/*.ts", "src/**/*.svelte"]
}

tsconfig.cm.json:

{
  "extends": "./tsconfig.json",
  "contentMappers": [{ "package": "@sveltejs/ts-content-mapper", "extensions": [".svelte"] }]
}

node_modules/my-lib/package.json:

{
  "name": "my-lib",
  "version": "1.0.0",
  "type": "module",
  "exports": { ".": { "types": "./dist/index.d.ts", "svelte": "./dist/index.js" } }
}

node_modules/my-lib/dist/index.d.ts (and index.js): export { default as Button } from './Button.svelte';

node_modules/my-lib/dist/Button.svelte.d.ts:

declare const Button: import('svelte').Component<{ label: string }, {}, ''>;
type Button = ReturnType<typeof Button>;
export default Button;

node_modules/my-lib/dist/Button.svelte, with an error that only its source has:

<script lang="ts">
    let { label }: { label: string } = $props();
    const width: number = label;
</script>

<button style:width>{label}</button>

src/main.ts:

import { Button } from "my-lib";

export const button: typeof Button = Button;
node node_modules/@typescript/native/bin/tsc -p tsconfig.json --noEmit --pretty false
node node_modules/@typescript/native/bin/tsc -p tsconfig.cm.json --noEmit --pretty false --runExternalCode

The first passes. The second reports:

node_modules/my-lib/dist/Button.svelte(3,11): error TS2322: Type 'string' is not assignable to type 'number'.

svelte-check 4.7.7's default mode passes too.

The resolver takes a fully specified import of a mapped extension as the file itself, before any declaration file: resolver.go#L1758-L1764, with Button.svelte.d.ts only tried afterwards (#L1625-L1637). The language server prefers .svelte.d.ts on purpose (svelte-sys.ts#L20-L23, #2481). Is this something to raise with TypeScript, or is there a way for the mapper to prefer the declaration?

I tried the resolver change this would need, on microsoft/TypeScript 98add1bbed (the 22 Oct nightly's commit), in tsc/internal/module/resolver.go: a mapped file's declarations (X.d.svelte.ts, then X.svelte.d.ts) are tried before the mapped X.svelte.

Diff (+13/-6)
diff --git a/tsc/internal/module/resolver.go b/tsc/internal/module/resolver.go
index 4b59f0582f..5a097b20d1 100644
--- a/tsc/internal/module/resolver.go
+++ b/tsc/internal/module/resolver.go
@@ -1756,15 +1756,22 @@ func (r *resolutionState) tryAddingExtensions(extensionless tspath.FileNameStem,
 		}
 		return continueSearching()
 	default:
-		if slices.Contains(r.resolver.extraExtensions, originalExtension) {
-			// A fully specified import of an extraExtension resolves directly to the file.
-			if resolved := r.tryExtension(originalExtension, extensionless, false); !resolved.shouldContinueSearching() {
-				resolved.resolvedUsingExtraExtensions = true
+		if extensions&extensionsDeclaration != 0 && !extensionless.AppendSuffix(originalExtension).IsDeclarationFile() {
+			if resolved := r.tryExtension(".d"+originalExtension+".ts", extensionless, false); !resolved.shouldContinueSearching() {
 				return resolved
 			}
 		}
-		if extensions&extensionsDeclaration != 0 && !extensionless.AppendSuffix(originalExtension).IsDeclarationFile() {
-			if resolved := r.tryExtension(".d"+originalExtension+".ts", extensionless, false); !resolved.shouldContinueSearching() {
+		if slices.Contains(r.resolver.extraExtensions, originalExtension) {
+			// A declaration file beside a mapped file describes it, as foo.d.ts describes foo.js:
+			// packages ship the mapped source their bundlers need next to its declarations.
+			if extensions&extensionsDeclaration != 0 {
+				if resolved := r.tryExtension(tspath.ExtensionDts, extensionless.AppendSuffix(originalExtension).AsStem(), false); !resolved.shouldContinueSearching() {
+					return resolved
+				}
+			}
+			// A fully specified import of an extraExtension resolves directly to the file.
+			if resolved := r.tryExtension(originalExtension, extensionless, false); !resolved.shouldContinueSearching() {
+				resolved.resolvedUsingExtraExtensions = true
 				return resolved
 			}
 		}

Built with npx hereby build. On our app, with this PR's mapper and tsc --runExternalCode, the npm nightly (7.1.0-dev.20261024.1) and the unpatched build both report 166 errors. The patched build removes exactly the 124 library errors (123 in layerchart, 1 in bits-ui), adds none, and still reports the deliberate errors we keep in one of the app's .svelte files and one .ts file. The 42 left are this PR's known gaps (SvelteKit auto-typing, and the const generics TS1277 above). It also fixes the repro above, and an app component without a declaration is still checked through the mapper.

Whether mapped files should prefer their declarations everywhere, as .js files do, or only in node_modules is the TypeScript team's call; this patch does it everywhere.

@jasonlyu123

Copy link
Copy Markdown
Member Author

In content mapper mode, TypeScript takes full control of module resolution logic. You can open an issue in TypeScript. That's indeed kind of a surprising result.

For the const modifier problem. It's not related to content mapper. It's just hidden in the current svelte-check because it can't be mapped to the source file. I think the const modifier can be removed from the generated type alias. But that would be a separate PR.

@jasonlyu123

jasonlyu123 commented Oct 24, 2026 •

Copy link
Copy Markdown
Member Author

For the module resolution problem. Now that I think of it. I guess that means that content mapper files are treated like a .ts file. It takes priority over declaration files and was not expected to be in the published libary. Svelte is the uncommon one that needs Svelte files to be shipped in component libraries.

Although not ideal, it's also not a complete blocker. Component libraries can still rely on the package.json export-map to remap the typing to .d.svelte.ts or .svelte.d.ts. The declaration files emitted by TypeScript 7.1 in content-mapper mode are .d.svelte.ts. And .svelte.d.ts might not work in NodeNext. Even if the priority is swept, a lot of the libraries might also need to migrate. Or remap it through an export map.

@jasonlyu123

Copy link
Copy Markdown
Member Author

Ah. Wait. It's still a problem. Export map won't work for barrel export.

@xyrolle

xyrolle commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

We tried a few ways around the barrel case (7.1 nightly with the mapper at this PR's head, bundler and NodeNext):

  • In the mapper: when a file under node_modules has a .svelte.d.ts beside it, return a re-export of that declaration instead of the transformed source. It fixes barrels and deep imports today without a TypeScript change, and matches what svelte-sys.ts does in the current mode. Open points: the transform cache doesn't see the .d.ts, and go-to-definition through the re-export.
  • In @sveltejs/package: emit ./X.svelte.js instead of ./X.svelte in the generated .d.ts. TypeScript's .js → .d.ts lookup then finds the declaration with or without the mapper, and it also fixes NodeNext with plain tsc today. It only helps packages once they republish.
  • In TypeScript: preferring declarations for mapped files inside node_modules fixes both, but a .ts file already beats its .d.ts there, so that's likely a harder sell.

Happy to share any of the patches if one is useful.

@bfanger

bfanger commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

@jasonlyu123 You linked my PR, but when I tested this ts7-content-mapper branch with a packages/svelte-check/test-success/TsNoCheck.svelte containing the example:

<script lang="ts">
    //@ts-nocheck
    let x: number = 'not a number';
</script>

Running the svelte-check tests resulted in:

FAIL: clean project --tsgo
        expected 0 errors, got 1

Seems our PRs are solving different issues

@jasonlyu123

jasonlyu123 commented Oct 28, 2026 •

Copy link
Copy Markdown
Member Author

Oh yeah. The // @ts-check hoisting logic in this branch is under a new flag, but it isn't enabled in svelte-check --tsgo/--incremental. (Won't push commit just yet. Might instead remove the flag #3111 (comment))

One of the reasons I linked this in your PR is that the original language server logic is slightly flawed. It only looks at the first comment. But in ts/js files, the last one wins.

@andrewbranch

Copy link
Copy Markdown

The module resolution behavior you're describing isn't really related to content mappers at all; the exact same thing happens with .ts vs. .d.ts files. When a library wants to publish their .ts files (e.g. for debug/inspection purposes), they're required to use outDir or other build tooling to create a structure that avoids the issue:

pkg/
  src/
    main.ts
  dist/
    main.js
    main.d.ts
    main.d.ts.map

If you're publishing .svelte files in libraries, and TypeScript is now configured to understand them as if they're .ts files, this constraint now applies to you.

One thing I'm considering is letting tsconfig content mapper configurations specify a glob for which files should be considered, instead of all files with matching extension (microsoft/TypeScript#64362). It seems like that would be useful here, if it's already common for libraries to publish .svelte files as siblings of declaration files. I would still recommend changing the publishing convention, but the glob could be an escape hatch for working with existing libraries. Do you think that would address the issue here?

@dummdidumm

Copy link
Copy Markdown
Member

Huh, yeah that's kind of interesting - we have to provide the .svelte files because that's what the Svelte compiler will need/use to generate the final app. So the svelte export condition will always point to that source, and we can't not publish it. There simply is no "generated" variant of a Svelte file like it is for .ts->.js. So if content mappers make TS understand Svelte as TS files, and therefore have TS always pick .svelte over .d.svelte.ts/svelte.d.ts that's a problem. Though we only want this to happen in node_modules. So yeah it's kind of weird, .svelte acts as both the uncompiled .ts and compiled .js.

If I understand you correctly publishing .svelte files is fine as long as it's not a sibling to the declaration file, because even though the types condition will make TypeScript look for that file declaration file, it still finds the "original TS" (in this case a .svelte file) next to it, and will use that instead?

// output folder
dist/
   Foo.svelte
   Foo.svelte.d.ts (or Foo.d.svelte.ts)

// exports map:
"./foo": {
  "types": "./dist/Foo.svelte.d.ts",
  "svelte": "./dist/Foo.svelte"
}
@andrewbranch

Copy link
Copy Markdown

If I understand you correctly publishing .svelte files is fine as long as it's not a sibling to the declaration file, because even though the types condition will make TypeScript look for that file declaration file, it still finds the "original TS" (in this case a .svelte file) next to it, and will use that instead?

Yes, basically, and moreover, the export map isn't relevant when a resolver is processing a relative import of another file inside your library. So even if you can make the export map do what you want for an import of "svelte-component-lib", it's not going to matter if the entrypoint of that library contains import OtherComponent from "./OtherComponent.svelte".

But again, this is no problem. Many libraries publish TS in addition to their compiled outputs for DX reasons, and the same resolution pattern happens in some monorepo workspaces, depending on configuration. This is one of the reasons we tend to recommend using a separate outDir.

(Aside: Foo.svelte.d.ts and Foo.d.svelte.ts are not interchangeable. We should leave the former completely out of the discussion and no tooling today should be using that as a declaration file for Foo.svelte. The latter has been supported since TypeScript 5.0 which is over 3 years old now and should have made the Foo.svelte.d.ts workaround obsolete.)

@dummdidumm

Copy link
Copy Markdown
Member

Ok so we'll have to adjust @sveltejs/package to separate the declaration files from the actual files, and to emit .d.svelte.ts instead of .svelte.d.ts . This will only work for libraries that update/republish though, so for the others we have to find workarounds.

@dummdidumm dummdidumm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic work! Merging this so the diff doesn't grow super large, we'll follow up with more PRs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

5 participants