Skip to content

Using with Node

Alexandre Rogozine Jr edited this page Dec 10, 2019 · 3 revisions

Using LINQ To Typescript with Node

Setting Up

Ensure that you're using Current or LTS version of Node

Run npm init to setup the project.

Run npm install linq-to-typescript tslib.

Using with JavaScript

Create index.js with the following,

"use strict";
const linq_to_typescript_1 = require("linq-to-typescript");

const primeNumbers = linq_to_typescript_1.range(2, 10000)
    .select((i) => [i, Math.floor(Math.sqrt(i))])
    .where(([i, iSq]) => linq_to_typescript_1.range(2, iSq).all((j) => i % j !== 0))
    .select(([prime]) => prime)
    .toArray();
console.log(primeNumbers);

then run node index.js

Using with TypeScript

Ensure that you have TypeScript installed npm install typescript -g

Run tsc --init to create the tsconfig.json file

Configuring tsconfig.json

  • Update the Typescript configuration file to target es2017. Node supports ES2017.
  • Add the ES2017, esnext.asynciterable, and dom library files to be included for compilation. ("lib" element)
  • Uncomment the "importHelpers": true line

Add index.ts file with the following,

import { range } from "linq-to-typescript"

const primeNumbers = range(2, 10000)
    .select((i) => [i, Math.floor(Math.sqrt(i))])
    .where(([i, iSq]) =>
        range(2, iSq).all((j) => i % j !== 0))
    .select(([prime]) => prime)
    .toArray()

async function asyncIterable() {
    for await (const i of  range(0, 10).selectAsync(async (x) => x * 2)) {
        console.log(i)
    }
}

asyncIterable()
console.log(primeNumbers)

Run tsc to compile the index.ts to index.js

Run node index.js

Clone this wiki locally