Skip to content

Comments

Add TypeScript port of TiaCodeGen library#21

Merged
jogibear9988 merged 3 commits intomainfrom
copilot/create-typescript-version
Feb 21, 2026
Merged

Add TypeScript port of TiaCodeGen library#21
jogibear9988 merged 3 commits intomainfrom
copilot/create-typescript-version

Conversation

Copy link
Contributor

Copilot AI commented Feb 21, 2026

TiaCodeGen had no JavaScript/TypeScript target, limiting its use in web/Node.js toolchains. This PR adds a complete TypeScript port in TiaCodegen-ts/ mirroring the C# project structure and API.

New project: TiaCodegen-ts/

  • Project setup: package.json + tsconfig.json with TypeScript 5.4, Jest for tests
  • StringBuilder: Custom implementation (utils/StringBuilder.ts) since no native equivalent exists in JS
  • All classes ported with identical inheritance hierarchy: BaseOperationOrSignal, coils, comparisons, arithmetic/timer/counter function blocks, Block/Network/CodeBlock, KopCodeHelper
  • index.ts: Barrel export of all public classes/interfaces

Key C# → TypeScript translations

C# TypeScript
ref int id { value: number } pass-by-reference object
StringBuilder Custom class (appendLine, append, toString)
params T[] ...args: T[] rest parameters
Multiple constructors Single constructor with runtime typeof/instanceof guards
LINQ (Select, Where, OrderBy, Flatten) map, filter, sort, recursive flatMap
Dictionary<string, T> Record<string, T> (V8 preserves insertion order for string keys)
GetType().Name / is T constructor.name / instanceof

Example

import { Block, CodeBlock, Network, And, Signal, Distributor, RCoil, Coil } from 'tia-codegen-ts';

const codeBlock = new CodeBlock();
const nw = new Network('Test1', 'Test1en');
nw.add(
  new And(
    new Signal('#Bool1'),
    new Distributor(
      new RCoil(new Signal('#Bool2')),
      new Coil(new Signal('#Bool3'))
    )
  )
);
codeBlock.add(nw);

const block = new Block('Test', 'title', codeBlock);
const xml = block.getCode();

Also fixes a bug present in the C# source: InRangeCall/OutRangeCall incorrectly used Direction.Output for the max parameter — corrected to Direction.Input.

Original prompt

This section details on the original issue you should resolve

<issue_title>Create a Typescript version</issue_title>
<issue_description>We should generate a Typescript version with nearly the same API.

  • We should generate a class for StringBuilder wich does not exist in Javascript, with the same API as in C# (only the methods we use).
  • We should replace the Linq commands with the counterparts in javascript (for example select => map, where => filter, ...)
  • We have some constructor overloads, those we need to filter at runtime
  • We should implement all classes, and also keep the class inheritance the same (if possible)
  • Create the project in a new folder with the same folder structure</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits February 21, 2026 13:58
Complete TypeScript translation of the TiaCodeGen library in TiaCodegen-ts/
mirroring the C# project structure.

Key translation decisions:
- ref int id -> { value: number } pass-by-reference object
- StringBuilder -> custom class with appendLine/append/toString
- C# Substring(start, length) -> TypeScript slice(start, start+length)
- Dictionary<string,T> -> Record<string,T> for insertion-order preservation
- Multiple constructors -> single constructor with runtime type checks
- ulong -> number
- LINQ -> Array methods (map/filter/flatMap/etc.)
- Abstract classes, interfaces and inheritance preserved

All 15 Jest tests pass, TypeScript compiles with no errors.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: jogibear9988 <364896+jogibear9988@users.noreply.github.com>
Copilot AI changed the title [WIP] Create Typescript version with similar API Add TypeScript port of TiaCodeGen library Feb 21, 2026
Copilot AI requested a review from jogibear9988 February 21, 2026 14:04
@jogibear9988 jogibear9988 marked this pull request as ready for review February 21, 2026 14:18
@jogibear9988 jogibear9988 merged commit 8b4f68d into main Feb 21, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create a Typescript version

2 participants