Skip to content

Commit 97f263c

Browse files
chore:Update for TS7 (#16485)
* Update for TS7 I am testing Typescript 7's JS support, which I've largely rewritten during the switch to Go. That means Javascript code will need to change much more than Typescript code. Fortunately, most of the changes are for the better: Javascript semantics are now nearly identical to Typescript semantics. It's much stricter and no longer has some persistent bugs that arose from shady JS handling I wrote years ago. This PR changes Svelte so that it compiles both with TS5.* and TS7, which means that occasionally there are duplicative or non-obvious changes. I'll annotate the interesting changes to explain why I made them. Because TS7 is quite a way off, I don't know whether you'll want to take this PR. Most of the changes are for the better, because they're due to stricter TS-aligned checking. But some are neutral and there is the previously mentioned duplication in a few places. * add changeset * revert scribbles mistakenly added * pnpm run format * revert mistaken edit * return to function declarations w/type tag * add job to ci.yml * skipLibCheck for now * no need for a changeset here --------- Co-authored-by: Rich Harris <[email protected]> Co-authored-by: Rich Harris <[email protected]>
1 parent 9efe672 commit 97f263c

File tree

28 files changed

+125
-68
lines changed

28 files changed

+125
-68
lines changed

.github/workflows/ci.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,23 @@ jobs:
6060
env:
6161
CI: true
6262
SVELTE_NO_ASYNC: true
63+
TSGo:
64+
permissions: {}
65+
runs-on: ubuntu-latest
66+
timeout-minutes: 5
67+
steps:
68+
- uses: actions/checkout@v4
69+
- uses: pnpm/action-setup@v4
70+
- uses: actions/setup-node@v4
71+
with:
72+
node-version: 24
73+
cache: pnpm
74+
- name: install
75+
run: pnpm install --frozen-lockfile
76+
- name: install tsgo
77+
run: cd packages/svelte && pnpm i -D @typescript/native-preview
78+
- name: type check
79+
run: cd packages/svelte && pnpm check:tsgo
6380
Lint:
6481
permissions: {}
6582
runs-on: ubuntu-latest

packages/svelte/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
"build": "node scripts/process-messages && rollup -c && pnpm generate:types && node scripts/check-treeshakeability.js",
142142
"dev": "node scripts/process-messages -w & rollup -cw",
143143
"check": "tsc --project tsconfig.runtime.json && tsc && cd ./tests/types && tsc",
144+
"check:tsgo": "tsgo --project tsconfig.runtime.json --skipLibCheck && tsgo --skipLibCheck",
144145
"check:watch": "tsc --watch",
145146
"generate:version": "node ./scripts/generate-version.js",
146147
"generate:types": "node ./scripts/generate-types.js && tsc -p tsconfig.generated.json",

packages/svelte/scripts/generate-types.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ await createBundle({
2626
// so that types/properties with `@internal` (and its dependencies) are removed from the output
2727
stripInternal: true,
2828
paths: Object.fromEntries(
29-
Object.entries(pkg.imports).map(([key, value]) => {
30-
return [key, [value.types ?? value.default ?? value]];
31-
})
29+
Object.entries(pkg.imports).map(
30+
/** @param {[string,any]} import */ ([key, value]) => {
31+
return [key, [value.types ?? value.default ?? value]];
32+
}
33+
)
3234
)
3335
},
3436
modules: {

packages/svelte/src/compiler/legacy.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ export function convert(source, ast) {
5555

5656
// Insert svelte:options back into the root nodes
5757
if (/** @type {any} */ (options)?.__raw__) {
58-
let idx = node.fragment.nodes.findIndex((node) => options.end <= node.start);
58+
let idx = node.fragment.nodes.findIndex(
59+
(node) => /** @type {any} */ (options).end <= node.start
60+
);
5961
if (idx === -1) {
6062
idx = node.fragment.nodes.length;
6163
}

packages/svelte/src/compiler/phases/2-analyze/css/css-prune.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import {
99
import { regex_ends_with_whitespace, regex_starts_with_whitespace } from '../../patterns.js';
1010
import { get_attribute_chunks, is_text_attribute } from '../../../utils/ast.js';
1111

12-
/** @typedef {NODE_PROBABLY_EXISTS | NODE_DEFINITELY_EXISTS} NodeExistsValue */
13-
/** @typedef {FORWARD | BACKWARD} Direction */
12+
/** @typedef {typeof NODE_PROBABLY_EXISTS | typeof NODE_DEFINITELY_EXISTS} NodeExistsValue */
13+
/** @typedef {typeof FORWARD | typeof BACKWARD} Direction */
1414

1515
const NODE_PROBABLY_EXISTS = 0;
1616
const NODE_DEFINITELY_EXISTS = 1;

packages/svelte/src/compiler/phases/2-analyze/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ export function analyze_module(source, options) {
297297
// TODO the following are not needed for modules, but we have to pass them in order to avoid type error,
298298
// and reducing the type would result in a lot of tedious type casts elsewhere - find a good solution one day
299299
ast_type: /** @type {any} */ (null),
300-
component_slots: new Set(),
300+
component_slots: /** @type {Set<string>} */ (new Set()),
301301
expression: null,
302302
function_depth: 0,
303303
has_props_rune: false,

packages/svelte/src/compiler/phases/2-analyze/utils/check_graph_for_cycles.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default function check_graph_for_cycles(edges) {
1414
}, new Map());
1515

1616
const visited = new Set();
17+
/** @type {Set<T>} */
1718
const on_stack = new Set();
1819
/** @type {Array<Array<T>>} */
1920
const cycles = [];

packages/svelte/src/compiler/phases/2-analyze/visitors/shared/a11y/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ function has_disabled_attribute(attribute_map) {
599599
/**
600600
* @param {string} tag_name
601601
* @param {Map<string, AST.Attribute>} attribute_map
602-
* @returns {ElementInteractivity[keyof ElementInteractivity]}
602+
* @returns {typeof ElementInteractivity[keyof typeof ElementInteractivity]}
603603
*/
604604
function element_interactivity(tag_name, attribute_map) {
605605
if (

packages/svelte/src/compiler/phases/2-analyze/visitors/shared/component.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ export function visit_component(node, context) {
145145
if (slot_name !== 'default') comments = [];
146146
}
147147

148+
/** @type {Set<string>} */
148149
const component_slots = new Set();
149150

150151
for (const slot_name in nodes) {

packages/svelte/src/compiler/phases/scope.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const NUMBER = Symbol('number');
2222
const STRING = Symbol('string');
2323
const FUNCTION = Symbol('string');
2424

25-
/** @type {Record<string, [type: NUMBER | STRING | UNKNOWN, fn?: Function]>} */
25+
/** @type {Record<string, [type: typeof NUMBER | typeof STRING | typeof UNKNOWN, fn?: Function]>} */
2626
const globals = {
2727
BigInt: [NUMBER],
2828
'Math.min': [NUMBER, Math.min],

0 commit comments

Comments
 (0)