typenative 0.5.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (149) hide show
  1. package/.github/workflows/ci.yml +2 -2
  2. package/.github/workflows/npm-publish-github-packages.yml +2 -2
  3. package/CHANGELOG.md +339 -0
  4. package/CONTRIBUTION.md +2 -1
  5. package/README.md +16 -7
  6. package/bin/cli.js +1 -0
  7. package/bin/index.js +62 -3
  8. package/bin/main.js +1 -0
  9. package/bin/node/core.js +46 -0
  10. package/bin/node/events.js +154 -15
  11. package/bin/node/http.js +1265 -127
  12. package/bin/node/process.js +13 -0
  13. package/bin/node/punycode.js +2 -2
  14. package/bin/node/stream.js +56 -7
  15. package/bin/node/stream_web.js +2 -2
  16. package/bin/node/timers.js +4 -2
  17. package/bin/node/url.js +28 -3
  18. package/bin/parse-node.js +2 -2
  19. package/bin/regex_vm.js +2105 -0
  20. package/bin/transpiler.js +17970 -4419
  21. package/bin/typecheck-node.js +2 -1
  22. package/bin/version.js +1 -1
  23. package/conformance.md +57 -15
  24. package/examples/README.md +50 -28
  25. package/examples/angular-templates/README.md +141 -0
  26. package/examples/angular-templates/bench.mjs +173 -0
  27. package/examples/angular-templates/dom-type-globals.d.ts +21 -0
  28. package/examples/angular-templates/dom-type-stubs.ts +21 -0
  29. package/examples/angular-templates/main.ts +135 -0
  30. package/examples/angular-templates/parity.mjs +157 -0
  31. package/examples/angular-templates/run-node.mjs +69 -0
  32. package/examples/angular-templates/vendor/angular/compiler/LICENSE +21 -0
  33. package/examples/desktop-notes/README.md +11 -13
  34. package/examples/dupe-finder/README.md +7 -5
  35. package/examples/fibonacci/README.md +7 -7
  36. package/examples/hello-world/README.md +6 -6
  37. package/examples/json-server/README.md +22 -18
  38. package/examples/mandelbrot-png/README.md +11 -13
  39. package/examples/marked-cli/README.md +60 -44
  40. package/examples/marked-cli/main.ts +122 -4
  41. package/examples/openai-chat/README.md +148 -0
  42. package/examples/openai-chat/bench.mjs +175 -0
  43. package/examples/openai-chat/main.ts +488 -0
  44. package/examples/react-compiler/README.md +185 -0
  45. package/examples/react-compiler/bench.mjs +181 -0
  46. package/examples/react-compiler/main.ts +324 -0
  47. package/examples/react-compiler/parity.mjs +362 -0
  48. package/examples/react-compiler/run-node.mjs +90 -0
  49. package/examples/react-compiler/vendor/babel/LICENSE +22 -0
  50. package/examples/react-compiler/vendor/babel/helper-globals/data/builtin-lower.json +15 -0
  51. package/examples/react-compiler/vendor/babel/helper-globals/data/builtin-upper.json +51 -0
  52. package/examples/react-compiler/vendor/charcodes/package.json +25 -0
  53. package/examples/react-compiler/vendor/invariant/LICENSE +21 -0
  54. package/examples/react-compiler/vendor/invariant/invariant.cjs +51 -0
  55. package/examples/react-compiler/vendor/invariant/invariant.d.cts +11 -0
  56. package/examples/react-compiler/vendor/invariant/types/LICENSE +21 -0
  57. package/examples/react-compiler/vendor/jridgewell/gen-mapping/LICENSE +19 -0
  58. package/examples/react-compiler/vendor/jridgewell/resolve-uri/LICENSE +19 -0
  59. package/examples/react-compiler/vendor/jridgewell/sourcemap-codec/LICENSE +19 -0
  60. package/examples/react-compiler/vendor/jridgewell/trace-mapping/LICENSE +19 -0
  61. package/examples/react-compiler/vendor/js-tokens/LICENSE +21 -0
  62. package/examples/react-compiler/vendor/js-tokens/index.cjs +23 -0
  63. package/examples/react-compiler/vendor/js-tokens/index.d.cts +13 -0
  64. package/examples/react-compiler/vendor/jsesc/LICENSE-MIT.txt +20 -0
  65. package/examples/react-compiler/vendor/jsesc/jsesc.cjs +337 -0
  66. package/examples/react-compiler/vendor/jsesc/jsesc.d.cts +384 -0
  67. package/examples/react-compiler/vendor/jsesc/types/LICENSE +21 -0
  68. package/examples/react-compiler/vendor/picocolors/LICENSE +15 -0
  69. package/examples/react-compiler/vendor/picocolors/picocolors.cjs +75 -0
  70. package/examples/react-compiler/vendor/picocolors/picocolors.d.cts +5 -0
  71. package/examples/react-compiler/vendor/picocolors/types.d.ts +51 -0
  72. package/examples/react-compiler/vendor/react-compiler/LICENSE +21 -0
  73. package/examples/react-compiler/vendor/stubs/babel-core.ts +31 -0
  74. package/examples/react-compiler/vendor/stubs/debug.ts +18 -0
  75. package/examples/react-compiler/vendor/stubs/js-tokens.d.ts +13 -0
  76. package/examples/react-compiler/vendor/stubs/pretty-format.ts +18 -0
  77. package/examples/react-compiler/vendor/zod/LICENSE +21 -0
  78. package/examples/react-compiler/vendor/zod-validation-error/LICENSE +9 -0
  79. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/NonEmptyArray.ts +5 -0
  80. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/joinPath.ts +49 -0
  81. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/joinValues.ts +48 -0
  82. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/stringify.ts +50 -0
  83. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/titleCase.ts +6 -0
  84. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/MessageBuilder.ts +126 -0
  85. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/ValidationError.ts +39 -0
  86. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/custom.ts +12 -0
  87. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/errorMap.ts +78 -0
  88. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/index.ts +2 -0
  89. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidElement.ts +12 -0
  90. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidKey.ts +12 -0
  91. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidStringFormat.ts +131 -0
  92. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidType.ts +54 -0
  93. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidUnion.ts +12 -0
  94. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidValue.ts +40 -0
  95. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/notMultipleOf.ts +12 -0
  96. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/tooBig.ts +76 -0
  97. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/tooSmall.ts +76 -0
  98. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/types.ts +27 -0
  99. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/unrecognizedKeys.ts +27 -0
  100. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromError.ts +13 -0
  101. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromZodError.ts +61 -0
  102. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromZodIssue.ts +38 -0
  103. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/index.ts +20 -0
  104. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isValidationError.ts +5 -0
  105. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isValidationErrorLike.ts +8 -0
  106. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isZodErrorLike.ts +11 -0
  107. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/toValidationError.ts +20 -0
  108. package/examples/react-compiler/vendor.mjs +688 -0
  109. package/examples/todo-cli/README.md +9 -9
  110. package/examples/yaml-cli/README.md +50 -34
  111. package/examples/yaml-cli/main.ts +236 -14
  112. package/package.json +8 -5
  113. package/performance/README.md +196 -0
  114. package/performance/any-dynamic.ts +21 -0
  115. package/performance/any-record-view.ts +20 -0
  116. package/performance/arr-hof.ts +21 -0
  117. package/performance/arr-push-index.ts +15 -0
  118. package/performance/arr-sort.ts +21 -0
  119. package/performance/async-await.ts +24 -0
  120. package/performance/class-dispatch.ts +52 -0
  121. package/performance/events-emit.ts +27 -0
  122. package/performance/exc-throw-rare.ts +25 -0
  123. package/performance/exc-try-catch.ts +26 -0
  124. package/performance/fn-closures.ts +29 -0
  125. package/performance/fn-recursion-tree.ts +31 -0
  126. package/performance/gen-for-of.ts +46 -0
  127. package/performance/json-roundtrip.ts +27 -0
  128. package/performance/map-delete.ts +24 -0
  129. package/performance/map-string-keys.ts +22 -0
  130. package/performance/num-arith-loop.ts +14 -0
  131. package/performance/num-bitwise.ts +21 -0
  132. package/performance/num-mod-int.ts +16 -0
  133. package/performance/num-to-string.ts +16 -0
  134. package/performance/obj-alloc.ts +32 -0
  135. package/performance/regex-exec.ts +29 -0
  136. package/performance/regex-site.ts +28 -0
  137. package/performance/set-ops.ts +20 -0
  138. package/performance/str-char-scan.ts +35 -0
  139. package/performance/str-concat-plus.ts +18 -0
  140. package/performance/str-concat.ts +24 -0
  141. package/performance/str-short-scan.ts +26 -0
  142. package/performance/str-split-join.ts +23 -0
  143. package/performance/str-template.ts +16 -0
  144. package/performance/union-switch.ts +57 -0
  145. package/tsparser/check.go +2 -1
  146. package/tsparser/main.go +1 -0
  147. package/types/typenative-npm.d.ts +3 -2
  148. package/types/typenative.d.ts +42 -12
  149. package/ROUND2-HANDOFF.md +0 -200
@@ -0,0 +1,18 @@
1
+ // perf: growing strings with `s = s + x` (not `+=`), each pass starting empty
2
+ import { performance } from 'node:perf_hooks';
3
+
4
+ const passes = 1000;
5
+
6
+ const start = performance.now();
7
+ let total = 0;
8
+ for (let pass = 0; pass < passes; pass++) {
9
+ let lines = '';
10
+ for (let i = 0; i < 3000; i++) {
11
+ lines = lines + 'line ' + i + '\n';
12
+ }
13
+ total += lines.length;
14
+ }
15
+ const elapsed = performance.now() - start;
16
+
17
+ console.log(`checksum: ${total}`);
18
+ console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
@@ -0,0 +1,24 @@
1
+ // perf: growing ~20 KB strings with `s += x` and `s = s + …` (each pass starts empty)
2
+ import { performance } from 'node:perf_hooks';
3
+
4
+ const passes = 400;
5
+ const words = ['alpha', 'beta', 'gamma', 'delta'];
6
+
7
+ const start = performance.now();
8
+ let total = 0;
9
+ for (let pass = 0; pass < passes; pass++) {
10
+ let out = '';
11
+ for (let i = 0; i < 3000; i++) {
12
+ out += words[i & 3];
13
+ out += ' ';
14
+ }
15
+ let lines = '';
16
+ for (let i = 0; i < 300; i++) {
17
+ lines = lines + 'line ' + i + '\n';
18
+ }
19
+ total += out.length + lines.length;
20
+ }
21
+ const elapsed = performance.now() - start;
22
+
23
+ console.log(`checksum: ${total}`);
24
+ console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
@@ -0,0 +1,26 @@
1
+ // perf: `charCodeAt(i)` / `s[i]` scans of many short (< 512 byte) lines
2
+ import { performance } from 'node:perf_hooks';
3
+
4
+ const lines: string[] = [];
5
+ for (let i = 0; i < 2000; i++) {
6
+ lines.push(` key_${i}: value ${i * 3} "quoted text" # a comment that pads the line out a bit more ${i}`);
7
+ }
8
+
9
+ const start = performance.now();
10
+ let idents = 0;
11
+ let digits = 0;
12
+ let quotes = 0;
13
+ for (let pass = 0; pass < 100; pass++) {
14
+ for (const line of lines) {
15
+ for (let i = 0; i < line.length; i++) {
16
+ const c = line.charCodeAt(i);
17
+ if ((c >= 97 && c <= 122) || c === 95) idents++;
18
+ else if (c >= 48 && c <= 57) digits++;
19
+ else if (line[i] === '"') quotes++;
20
+ }
21
+ }
22
+ }
23
+ const elapsed = performance.now() - start;
24
+
25
+ console.log(`checksum: ${idents * 1000000 + digits * 1000 + quotes}`);
26
+ console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
@@ -0,0 +1,23 @@
1
+ // perf: `split`, `join`, `indexOf`, `slice` and `trim` on CSV-like lines
2
+ import { performance } from 'node:perf_hooks';
3
+
4
+ const lines: string[] = [];
5
+ for (let i = 0; i < 1000; i++) lines.push(` id${i}, name ${i}, ${i * 7}, tag-${i % 17}, done `);
6
+ const csv = lines.join('\n');
7
+
8
+ const start = performance.now();
9
+ let sum = 0;
10
+ for (let pass = 0; pass < 200; pass++) {
11
+ const rows = csv.split('\n');
12
+ for (const row of rows) {
13
+ const cells = row.split(',');
14
+ sum += cells.length;
15
+ const tag = cells[3].trim();
16
+ sum += tag.slice(tag.indexOf('-') + 1).length;
17
+ sum += cells.map((c) => c.trim()).join('|').length;
18
+ }
19
+ }
20
+ const elapsed = performance.now() - start;
21
+
22
+ console.log(`checksum: ${sum}`);
23
+ console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
@@ -0,0 +1,16 @@
1
+ // perf: template literals mixing strings, integers, floats and booleans
2
+ import { performance } from 'node:perf_hooks';
3
+
4
+ const n = 1_000_000;
5
+ const names = ['ada', 'grace', 'linus', 'ken'];
6
+
7
+ const start = performance.now();
8
+ let total = 0;
9
+ for (let i = 0; i < n; i++) {
10
+ const s = `${names[i & 3]}#${i}: ${i / 4} items, ok=${(i & 1) === 0}`;
11
+ total += s.length;
12
+ }
13
+ const elapsed = performance.now() - start;
14
+
15
+ console.log(`checksum: ${total}`);
16
+ console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
@@ -0,0 +1,57 @@
1
+ // perf: evaluating an expression tree of a discriminated union with `switch (e.kind)`
2
+ import { performance } from 'node:perf_hooks';
3
+
4
+ interface Num {
5
+ kind: 'num';
6
+ value: number;
7
+ }
8
+ interface Add {
9
+ kind: 'add';
10
+ left: Expr;
11
+ right: Expr;
12
+ }
13
+ interface Mul {
14
+ kind: 'mul';
15
+ left: Expr;
16
+ right: Expr;
17
+ }
18
+ interface Neg {
19
+ kind: 'neg';
20
+ operand: Expr;
21
+ }
22
+ type Expr = Num | Add | Mul | Neg;
23
+
24
+ function build(depth: number, seed: number): Expr {
25
+ if (depth === 0) return { kind: 'num', value: (seed % 10) + 1 };
26
+ switch (seed % 3) {
27
+ case 0:
28
+ return { kind: 'add', left: build(depth - 1, seed * 3 + 1), right: build(depth - 1, seed + 7) };
29
+ case 1:
30
+ return { kind: 'mul', left: build(depth - 1, seed + 2), right: build(depth - 1, seed * 7 + 3) };
31
+ default:
32
+ return { kind: 'neg', operand: build(depth - 1, seed + 5) };
33
+ }
34
+ }
35
+
36
+ function evaluate(e: Expr): number {
37
+ switch (e.kind) {
38
+ case 'num':
39
+ return e.value;
40
+ case 'add':
41
+ return (evaluate(e.left) + evaluate(e.right)) % 1009;
42
+ case 'mul':
43
+ return ((evaluate(e.left) * evaluate(e.right)) % 1009) + 1;
44
+ case 'neg':
45
+ return 1010 - evaluate(e.operand);
46
+ }
47
+ }
48
+
49
+ const tree = build(18, 1);
50
+
51
+ const start = performance.now();
52
+ let total = 0;
53
+ for (let i = 0; i < 20; i++) total += evaluate(tree);
54
+ const elapsed = performance.now() - start;
55
+
56
+ console.log(`checksum: ${total}`);
57
+ console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
package/tsparser/check.go CHANGED
@@ -86,7 +86,8 @@ func runCheck(entry string, typesDir string) {
86
86
  }
87
87
  } else {
88
88
  options = &core.CompilerOptions{
89
- Target: core.ScriptTargetES2020,
89
+ Target: core.ScriptTargetES2025,
90
+ UseDefineForClassFields: core.TSFalse,
90
91
  Module: core.ModuleKindESNext,
91
92
  ModuleResolution: core.ModuleResolutionKindBundler,
92
93
  Lib: []string{},
package/tsparser/main.go CHANGED
@@ -359,4 +359,5 @@ func lowerFirst(s string) string {
359
359
  // Field renames to match the TypeScript API's names.
360
360
  var fieldRenames = map[string]string{
361
361
  "PostfixToken": "questionToken", // optional property `a?: string`
362
+ "DefaultType": "default", // type parameter default `<S = any>`
362
363
  }
@@ -77,6 +77,7 @@ declare module 'node:buffer' {
77
77
 
78
78
  declare module 'node:fs' {
79
79
  import { Readable, Writable } from 'node:stream';
80
+ import { EventEmitter } from 'node:events';
80
81
  namespace fs {
81
82
  type PathLike = string;
82
83
  type EncodingOption = BufferEncoding | { encoding?: BufferEncoding | null; flag?: string } | null;
@@ -247,7 +248,7 @@ declare module 'node:fs' {
247
248
  encoding?: BufferEncoding;
248
249
  signal?: AbortSignal;
249
250
  }
250
- interface FSWatcher extends import('node:events').EventEmitter {
251
+ interface FSWatcher extends EventEmitter {
251
252
  close(): void;
252
253
  ref(): this;
253
254
  unref(): this;
@@ -258,7 +259,7 @@ declare module 'node:fs' {
258
259
  once(event: 'error', listener: (error: Error) => void): this;
259
260
  once(event: 'close', listener: () => void): this;
260
261
  }
261
- interface StatWatcher extends import('node:events').EventEmitter {
262
+ interface StatWatcher extends EventEmitter {
262
263
  ref(): this;
263
264
  unref(): this;
264
265
  }
@@ -24,8 +24,11 @@ interface CallableFunction extends Function {
24
24
  }
25
25
  interface NewableFunction extends Function {}
26
26
 
27
- /** The global `Function` as a value: an opaque constructor (`x instanceof Function` checks for a function); calling it compiles code, which a native binary cannot */
28
- interface FunctionConstructor extends Function {}
27
+ /** The global `Function` as a value: an opaque constructor (`x instanceof Function` checks for a function). `new Function(...)` compiles to a binary that throws if the program ever calls the result: a native binary cannot evaluate dynamic code */
28
+ interface FunctionConstructor extends Function {
29
+ new (...args: string[]): Function;
30
+ (...args: string[]): Function;
31
+ }
29
32
  declare var Function: FunctionConstructor;
30
33
 
31
34
  interface IArguments {
@@ -40,6 +43,8 @@ interface Number {
40
43
  toExponential(fractionDigits?: number): string;
41
44
  }
42
45
 
46
+ type PropertyKey = string | number | symbol;
47
+
43
48
  interface Object {
44
49
  constructor: Function;
45
50
  toString(): string;
@@ -169,6 +174,7 @@ interface RegExp {
169
174
  readonly multiline: boolean;
170
175
  readonly dotAll: boolean;
171
176
  readonly unicode: boolean;
177
+ readonly unicodeSets: boolean;
172
178
  readonly sticky: boolean;
173
179
  test(string: string): boolean;
174
180
  exec(string: string): RegExpExecArray | null;
@@ -257,23 +263,23 @@ interface Array<T> extends IterableIterator<T> {
257
263
  /**
258
264
  * Performs the callback for each element.
259
265
  */
260
- forEach(callback: (value: T, index?: number, array?: T[]) => void): void;
266
+ forEach(callback: (value: T, index: number, array: T[]) => void): void;
261
267
  /**
262
268
  * Calls the callback function on each element of the array, accumulating a result.
263
269
  */
264
270
  reduce<U>(
265
- callback: (accumulator: U, value: T, index?: number, array?: T[]) => U,
271
+ callback: (accumulator: U, value: T, index: number, array: T[]) => U,
266
272
  initialValue: U
267
273
  ): U;
268
- reduce(callback: (accumulator: T, value: T, index?: number, array?: T[]) => T): T;
274
+ reduce(callback: (accumulator: T, value: T, index: number, array: T[]) => T): T;
269
275
  /**
270
276
  * Calls the callback function on each element of the array in reverse order, accumulating a result.
271
277
  */
272
278
  reduceRight<U>(
273
- callback: (accumulator: U, value: T, index?: number, array?: T[]) => U,
279
+ callback: (accumulator: U, value: T, index: number, array: T[]) => U,
274
280
  initialValue: U
275
281
  ): U;
276
- reduceRight(callback: (accumulator: T, value: T, index?: number, array?: T[]) => T): T;
282
+ reduceRight(callback: (accumulator: T, value: T, index: number, array: T[]) => T): T;
277
283
  /**
278
284
  * Returns the index of the first occurrence of a value in an array, or -1 if it is not present.
279
285
  */
@@ -329,8 +335,8 @@ interface Array<T> extends IterableIterator<T> {
329
335
  /**
330
336
  * Returns a new array formed by applying callback to each element and then flattening.
331
337
  */
332
- flat(): T[];
333
- flatMap<U>(callback: (value: T, index?: number, array?: T[]) => U[]): U[];
338
+ flat(depth?: number): T[];
339
+ flatMap<U>(callback: (value: T, index: number, array: T[]) => U[]): U[];
334
340
  /**
335
341
  * Returns a copy of a section of an array with the fill value applied.
336
342
  */
@@ -359,9 +365,14 @@ interface Array<T> extends IterableIterator<T> {
359
365
  [n: number]: T;
360
366
  }
361
367
 
368
+ /** The read-only view of a Set: the same values, no mutating members used */
369
+ type ReadonlySet<T> = Set<T>;
370
+
362
371
  interface ArrayConstructor {
363
372
  new <T>(length: number): T[];
364
373
  new <T>(...items: T[]): T[];
374
+ <T>(length: number): T[];
375
+ <T>(...items: T[]): T[];
365
376
  isArray(arg: any): arg is any[];
366
377
  from<T>(arrayLike: ArrayLike<T> | Iterable<T>): T[];
367
378
  from<T, U>(arrayLike: ArrayLike<T> | Iterable<T>, mapFn: (value: T, index: number) => U): U[];
@@ -395,11 +406,11 @@ interface String {
395
406
  isWellFormed(): boolean;
396
407
  toWellFormed(): string;
397
408
  /** Determines whether a string contains the specified substring. */
398
- includes(searchString: string): boolean;
409
+ includes(searchString: string, position?: number): boolean;
399
410
  /** Determines whether a string begins with the specified characters. */
400
- startsWith(searchString: string): boolean;
411
+ startsWith(searchString: string, position?: number): boolean;
401
412
  /** Determines whether a string ends with the specified characters. */
402
- endsWith(searchString: string): boolean;
413
+ endsWith(searchString: string, endPosition?: number): boolean;
403
414
  /** Splits a string into substrings using the specified separator. */
404
415
  split(separator: string | RegExp, limit?: number): string[];
405
416
  /** Removes whitespace from both ends of a string. */
@@ -633,6 +644,8 @@ interface ObjectConstructor {
633
644
  /** An instance's class (null for other values) */
634
645
  getPrototypeOf(obj: any): any;
635
646
  getOwnPropertyDescriptors<T>(obj: T): { [P in keyof T]: PropertyDescriptor } & { [key: string]: PropertyDescriptor };
647
+ getOwnPropertyDescriptor(obj: any, key: PropertyKey): PropertyDescriptor | undefined;
648
+ getOwnPropertySymbols(obj: any): symbol[];
636
649
  getOwnPropertyNames(obj: any): string[];
637
650
  freeze<T>(obj: T): T;
638
651
  seal<T>(obj: T): T;
@@ -755,6 +768,10 @@ interface ErrorOptions {
755
768
  interface ErrorConstructor {
756
769
  new (message?: string, options?: ErrorOptions): Error;
757
770
  (message?: string, options?: ErrorOptions): Error;
771
+ // (V8's: accepted, without effect — there are no V8 stack frames)
772
+ captureStackTrace(targetObject: object, constructorOpt?: Function): void;
773
+ stackTraceLimit: number;
774
+ prepareStackTrace?: (err: Error, stackTraces: any[]) => any;
758
775
  }
759
776
 
760
777
  declare var Error: ErrorConstructor;
@@ -1738,3 +1755,16 @@ declare namespace JSX {
1738
1755
  children: {};
1739
1756
  }
1740
1757
  }
1758
+
1759
+ // Reflect: the Object operations as functions
1760
+ declare namespace Reflect {
1761
+ function apply(target: Function, thisArgument: any, argumentsList: ArrayLike<any>): any;
1762
+ function defineProperty(target: object, propertyKey: PropertyKey, attributes: PropertyDescriptor): boolean;
1763
+ function deleteProperty(target: object, propertyKey: PropertyKey): boolean;
1764
+ function get(target: object, propertyKey: PropertyKey, receiver?: any): any;
1765
+ function getOwnPropertyDescriptor(target: object, propertyKey: PropertyKey): PropertyDescriptor | undefined;
1766
+ function getPrototypeOf(target: object): object | null;
1767
+ function has(target: object, propertyKey: PropertyKey): boolean;
1768
+ function ownKeys(target: object): (string | symbol)[];
1769
+ function set(target: object, propertyKey: PropertyKey, value: any, receiver?: any): boolean;
1770
+ }
package/ROUND2-HANDOFF.md DELETED
@@ -1,200 +0,0 @@
1
- # Round 2 handoff: marked
2
-
3
- You're picking up round 2 of 3 in a series of "flagship examples" for
4
- TypeNative (a TypeScript-to-Go transpiler, repo `danisss9/TypeNative`). Each
5
- round compiles a real, unmodified npm package's TypeScript source to a native
6
- binary and proves its output matches Node byte for byte. Round 1 (the `yaml`
7
- CLI) is done; round 3 is the Angular template parser. This round is
8
- [marked](https://github.com/markedjs/marked).
9
-
10
- If the original plan specifies something for round 2 that differs from this
11
- brief, the plan wins.
12
-
13
- ## Where things stand
14
-
15
- - Round 1 is on branch `claude/brave-ride-d67lzw`, PR
16
- [danisss9/TypeNative#30](https://github.com/danisss9/TypeNative/pull/30),
17
- open and already merged with current `main`. Each round ships as its own
18
- PR. If #30 hasn't merged when you start, branch from it (round 2 needs its
19
- transpiler fixes); otherwise branch from `main`. Don't open a PR unless the
20
- user asks.
21
- - Use `examples/yaml-cli/` as the template for everything:
22
- [main.ts](examples/yaml-cli/main.ts), [parity.mjs](examples/yaml-cli/parity.mjs),
23
- [bench.mjs](examples/yaml-cli/bench.mjs), [README.md](examples/yaml-cli/README.md),
24
- [vendor/yaml/VENDOR.md](examples/yaml-cli/vendor/yaml/VENDOR.md).
25
- - State at handoff: conformance 523/523 under both the Node CLI and the
26
- self-hosted native compiler; yaml parity 1608/1608; `bootstrap:check`
27
- holds (56 Go files identical).
28
-
29
- ## The target
30
-
31
- | | |
32
- | --- | --- |
33
- | Upstream | https://github.com/markedjs/marked |
34
- | Tag | `v18.1.2` (latest when this was written; use it unless the plan pins another) |
35
- | Commit | `5beecae42b0beb07eaccc3a732560f56867a8301` |
36
- | Source | `src/*.ts`, 11 files, about 4,070 lines, imports use `.ts` extensions |
37
- | CLI | `bin/main.js` (JavaScript; imports the built `lib/marked.esm.js`) |
38
- | License | MIT |
39
-
40
- Things in the source that will matter:
41
-
42
- - `src/rules.ts` is a large set of regexes (22 of them, two with lookbehind),
43
- many composed at run time with `.replace()` on their `.source`. Expect the
44
- regex translation and the backtracking engine to get a workout.
45
- - `Lexer` / `Tokenizer` / `Parser` / `Renderer` classes, hooks and
46
- extensions (`Instance.ts`), with `this`-bound callbacks.
47
- - `bin/main.js` uses `fs.promises`, `createRequire`, `pathToFileURL`, dynamic
48
- imports of `child_process` / `url`, and spawns `man` for `--help`.
49
-
50
- ## Deliverables (same rules as round 1)
51
-
52
- Layout: `examples/marked-cli/`
53
-
54
- - `vendor/marked/`: `src/` and `LICENSE` copied byte for byte, plus
55
- `VENDOR.md` with the tag, the commit and the patch list. The target is an
56
- empty patch list.
57
- - `main.ts`: a TypeScript port of `bin/main.js`, importing
58
- `./vendor/marked/src/marked.ts` instead of the built bundle. It must run
59
- under both `node main.ts` and TypeNative. For `--help`, matching Node's
60
- behavior (spawning `man`) isn't required for parity; just keep it working.
61
- - `parity.mjs`: runs every corpus case through Node and the native binary,
62
- compares stdout, stderr and exit code byte for byte, prints the pass rate
63
- and writes failures to `.cache/failures/`. Copy yaml-cli's, including its
64
- `withoutStackFrames()` normalization (Node prints JS stack frames; a native
65
- binary has none).
66
- - `bench.mjs`: (a) per-call latency, (b) throughput, alternating Node and
67
- native runs, checking the outputs are identical.
68
- - `README.md` in the yaml-cli style. Report losses honestly too: yaml-cli is
69
- 27x faster per call but 3.6x slower on 10 MB, and its README says so.
70
-
71
- Corpora are fetched into a gitignored `.cache/` (`examples/*/.cache` is
72
- already ignored), never committed.
73
-
74
- **Proposed corpus** (confirm against the plan): marked's own spec suites at
75
- the vendored tag.
76
-
77
- - `test/specs/commonmark/commonmark.0.31.2.json`: 652 cases.
78
- - `test/specs/gfm/gfm.0.29.json`: 28 cases.
79
- - `test/specs/new/*.md`: about 223 cases.
80
- - `test/specs/original/*.md`: 22 cases.
81
- - `test/specs/redos/*`: 36 inputs, worth including with the 10 s timeout,
82
- since they will expose any super-linear regex path.
83
-
84
- Feed each case's markdown on stdin, in at least the default (GFM) mode and
85
- with `--no-gfm` if the CLI supports it. Parity means native vs Node, not
86
- native vs the spec's expected HTML; spec cases marked failing in marked
87
- don't matter.
88
-
89
- **Proposed benchmarks**:
90
-
91
- - (a) A typical README (e.g. marked's own `README.md`) to HTML, 30 runs.
92
- - (b) About 10 MB of generated markdown (headings, lists, tables, code
93
- fences, links, emphasis) from a fixed-seed generator, 5 runs.
94
-
95
- ## Hard rules
96
-
97
- - No patches to upstream code. When a construct doesn't compile or behaves
98
- differently, fix the transpiler generally. The only exception is something
99
- `conformance.md` marks 🚫, documented in `VENDOR.md` with the reason.
100
- - Every fix gets a probe in `conformance/` (it runs under TypeNative with an
101
- `assert` global) and a row in `conformance.md` (✅ with the probe link; ❌
102
- rows for gaps you find but don't fix).
103
- - The self-hosted compiler must keep working: `npm run build:native &&
104
- npm run bootstrap:check`.
105
- - Docs per round: a row in `examples/README.md`, a line in the root README's
106
- Examples section, and a CHANGELOG entry under `[Unreleased]`.
107
- - Commit trailer, and never put a model name in commits, PRs or code:
108
- ```
109
- Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
110
- Claude-Session: https://claude.ai/code/session_012GsG7QUSC36HGJ9rohJ7A7
111
- ```
112
-
113
- ## Verification (run all of it before pushing)
114
-
115
- 1. `npm run build`
116
- 2. Compile the example: `node bin/cli.js --source examples/marked-cli/main.ts --output …`
117
- 3. `cd examples/marked-cli && node parity.mjs` must report 100%.
118
- 4. `node bench.mjs`
119
- 5. `npm run conformance:parallel`
120
- 6. `npm run build:native && npm run bootstrap:check`
121
- 7. `node scripts/conformance.mjs --native`
122
-
123
- ## Environment
124
-
125
- ```bash
126
- export PATH=/opt/nvm/versions/node/v24.22.2/bin:$PATH
127
- export GOTOOLCHAIN=go1.26.0
128
- ```
129
-
130
- Without these you get "TypeNative needs Go 1.26 or newer" or an old Node.
131
- Timings in this container: conformance:parallel about 7 min,
132
- `conformance --native` about 5 min, bootstrap:check about 75 s,
133
- build:native about 3 min.
134
-
135
- ## Lessons from round 1 (read these; they will save hours)
136
-
137
- **Working method**
138
-
139
- - Write a small `tn.sh file.ts` that runs a snippet under Node and natively
140
- and prints both. Most bugs reduce to five lines this way.
141
- - `node bin/cli.js --source x.ts --emit-go DIR` writes the Go. Strip the
142
- line directives with `sed 's#/\*line[^*]*\*/##g'` to read it.
143
- - For a native panic, the recovered error loses the stack. Patch the
144
- emitted Go's `TnCaught` to print `runtime/debug.Stack()`; import it under
145
- an alias (`rtdbg`), since vendored code may already have a `debug`.
146
- - Don't rebuild `bin/` while conformance runs: probes compile with it and
147
- fail spuriously.
148
- - Parity flakes are rare. If a parallel conformance job fails once but passes
149
- alone (fs-watch timing, the big example build under load), rerun it alone
150
- before chasing it.
151
-
152
- **Self-hosting traps.** These cost the most time in round 1. The native
153
- compiler (the transpiler compiled by itself) has known semantic gaps. Code
154
- you add to `src/transpiler.ts` must avoid them, or `bootstrap:check` fails:
155
-
156
- - `map.get(k)` on a missing key returns the zero value (`''`, `0`, `false`),
157
- not `undefined`. So `m.get(k) ?? fallback` and `if (m.get(k) !== undefined)`
158
- are wrong natively. Use `m.has(k) ? m.get(k) : undefined`. The same goes for
159
- `Record` lookups (`Object.hasOwn(rec, k) ? rec[k] : undefined`) and for
160
- `arr.find()`, which returns `''` for no match (use `filter()[0]` with a
161
- length check).
162
- - `bool || string` evaluates to a bool, not the string operand. Don't mix
163
- types in `||` / `&&` value positions.
164
- - A function returning `string | undefined` can yield `''` where JS yields
165
- `undefined`; prefer `||` over `??` when `''` is never a valid value (Go
166
- types never are).
167
- - Object literals assigned into `any` slots become Go maps; those can now be
168
- Map/Set keys (by identity), but don't rely on struct identity for them.
169
- - To find a divergence: emit the same program's Go with both compilers
170
- (`./native/typenative --emit-go` vs `node bin/cli.js --emit-go`) and diff
171
- them, after stripping line directives. The first differing line usually
172
- names the faulty inference rule.
173
- - Helpers `untypedVariable()`, `declaredMemberType()` and
174
- `getObjectFieldGoType()` already encode the `has()`-first pattern; reuse
175
- them.
176
-
177
- **Transpiler areas you'll likely touch for marked**
178
-
179
- - Regexes: `TnRegex` (RE2, with a backtracking engine for lookarounds and
180
- backreferences), sticky/global flags, `$` replacement patterns, replacer
181
- callbacks (match, groups…, offset, input) and unmatched groups as
182
- `undefined` all work now. `new RegExp(src, flags)` with `g`/`y` is
183
- stateful. Composed regexes (`.source` edited with `.replace`) are new
184
- territory.
185
- - String positions are code points (UTF-16 units in the BMP), with a cached
186
- index per long string. Long inputs scanned by regexes were quadratic until
187
- round 1 fixed the cache eviction; watch for similar hotspots with a
188
- profiler.
189
- - Profiling: patch the emitted Go's `main()` to call
190
- `pprof.StartCPUProfile` and `TnProcessExit` to stop it, then `go tool pprof
191
- -top`. Round 1's wins were the string index cache, cached struct field
192
- lookups, and resolving `typeof f` as a func type (direct calls instead of
193
- reflection).
194
-
195
- **Known gaps left open (❌ rows)**: a local alias of an array field doesn't
196
- share pushes; an array read from an `any` object into a variable isn't
197
- shared; `x?.m()` on an `any` value with a primitive result gives the zero
198
- value instead of `undefined`; reading a missing `Record` / `Map` key of
199
- primitives gives the zero value. Fix them if marked needs them, with
200
- probes.