{"id":11241,"library":"linq-to-typescript","title":"LINQ to TypeScript","description":"LinqToTypeScript is an active library, currently at stable version 12.1.0, that ports the Language Integrated Query (LINQ) functionality from .NET to TypeScript. It provides a comprehensive set of methods for querying, transforming, and manipulating collections, inspired by the IEnumerable, IAsyncEnumerable, and IParallelEnumerable interfaces. The library supports both synchronous and asynchronous operations, leveraging modern JavaScript features like `AsyncIterable` and `generators`. It targets TypeScript 5.6.X and ES2022, shipping exclusively as ECMAScript Modules (ESM). Key differentiators include its dual usage pattern (explicit `from` wrapper or extending native types via `initializeLinq`), support for parallel operations, and consistent release cadence with new LINQ methods being regularly added.","status":"active","version":"12.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/arogozine/LinqToTypeScript","tags":["javascript","typescript","esm","linq","async","generators","enumerable","es2022"],"install":[{"cmd":"npm install linq-to-typescript","lang":"bash","label":"npm"},{"cmd":"yarn add linq-to-typescript","lang":"bash","label":"yarn"},{"cmd":"pnpm add linq-to-typescript","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Used to wrap iterable data sources for synchronous LINQ operations. The package is ESM-only since v11.0.0, so CommonJS `require` is not supported.","wrong":"const { from } = require('linq-to-typescript')","symbol":"from","correct":"import { from } from 'linq-to-typescript'"},{"note":"A utility function that binds LINQ methods directly to native JavaScript types like `Array`, `Map`, `Set`, and `String`, allowing for a more fluent, extension-method-like syntax. Requires `declare global` type augmentations.","wrong":"const { initializeLinq } = require('linq-to-typescript')","symbol":"initializeLinq","correct":"import { initializeLinq } from 'linq-to-typescript'"},{"note":"The core interface representing a sequence of elements that can be queried. Primarily used for type declarations and when implementing custom enumerable types. It is a named export, not a default export.","wrong":"import IEnumerable from 'linq-to-typescript'","symbol":"IEnumerable","correct":"import { IEnumerable } from 'linq-to-typescript'"},{"note":"The interface for sequences that support asynchronous iteration. Essential for operations that involve promises or awaitable data sources.","symbol":"IAsyncEnumerable","correct":"import { IAsyncEnumerable } from 'linq-to-typescript'"}],"quickstart":{"code":"import { from, initializeLinq, IEnumerable } from 'linq-to-typescript';\n\n// 1. Declare that the JS types implement the IEnumerable interface\ndeclare global {\n    interface Array<T> extends IEnumerable<T> { }\n    interface String extends IEnumerable<string> { }\n}\n\n// 2. Bind Linq Functions to Array, Map, etc\ninitializeLinq();\n\nasync function demonstrateLinq() {\n  console.log('--- Using Wrapper (from) ---');\n  const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];\n  const evenNumbersWrapper = from(numbers)\n    .where((x) => x % 2 === 0)\n    .select((x) => x * 2)\n    .toArray();\n  console.log('Even numbers doubled (wrapper):', evenNumbersWrapper); // Expected: [4, 8, 12, 16, 20]\n\n  console.log('\\n--- Using Extended Native Types ---');\n  const oddNumbersNative = numbers.where((x) => x % 2 !== 0).toArray();\n  console.log('Odd numbers (native extension):', oddNumbersNative); // Expected: [1, 3, 5, 7, 9]\n\n  console.log('\\n--- Async Operations ---');\n  const asyncData = [10, 20, 30];\n  const processedAsyncData = await from(asyncData)\n    .selectAsync(async (x) => {\n      await new Promise(res => setTimeout(res, 50)); // Simulate async delay\n      return x + 1;\n    })\n    .toArray();\n  console.log('Processed async data:', processedAsyncData); // Expected: [11, 21, 31]\n}\n\ndemonstrateLinq();\n","lang":"typescript","description":"This quickstart demonstrates both the `from` wrapper and the native type extension pattern using `initializeLinq` for synchronous and asynchronous LINQ operations on arrays."},"warnings":[{"fix":"Migrate all `require()` statements to `import` statements. Ensure your `package.json` specifies `\"type\": \"module\"` or use a `.mjs` extension for files using ESM imports. Update `tsconfig.json` `module` option if necessary.","message":"Starting with v11.0.0, the package ships exclusively with ECMAScript Modules (ESM). CommonJS is no longer supported, requiring project configuration (e.g., `\"type\": \"module\"` in `package.json`) and `import` syntax.","severity":"breaking","affected_versions":">=11.0.0"},{"fix":"Review existing uses of `zip` to ensure compatibility with the .NET-standard behavior. If the old behavior is needed, implement custom logic.","message":"The `zip` method's behavior was updated in v10.0.0 to align with .NET's implementation when dealing with enumerations of different lengths, which might alter results for existing code relying on previous behavior.","severity":"breaking","affected_versions":">=10.0.0"},{"fix":"It is recommended to use TypeScript 5.6.X or newer. Ensure your `tsconfig.json` has `\"target\": \"es2022\"` and `\"lib\": [\"es2022\"]`.","message":"LinqToTypeScript v12.0.0 and above are built with TypeScript 5.6.X and target ES2022. While generally backward compatible, using significantly older TypeScript versions in your project might lead to type-checking or build issues.","severity":"gotcha","affected_versions":">=12.0.0"},{"fix":"Upgrade your Node.js environment to version 17 or higher. Use a Node.js version manager like `nvm` to easily switch versions.","message":"The package requires Node.js version 17 or newer due to its engine specification. Running in older Node.js environments will result in errors.","severity":"gotcha","affected_versions":">=10.0.0-beta2"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change `const myLinq = require('linq-to-typescript');` to `import * as myLinq from 'linq-to-typescript';` or `import { from } from 'linq-to-typescript';`. Ensure `\"type\": \"module\"` is set in your `package.json`.","cause":"Attempting to use CommonJS `require` syntax in a project configured for ECMAScript Modules (ESM) after LinqToTypeScript v11.","error":"ReferenceError: require is not defined"},{"fix":"Verify that `import { from } from 'linq-to-typescript';` is used and that your build tools (e.g., Webpack, Rollup, Babel) are configured to correctly handle ESM. Check `tsconfig.json` for `moduleResolution` and `module` settings.","cause":"This error typically occurs when there's a module resolution mismatch or incorrect import statement, especially in environments transpiling ESM to CJS, or when `from` is not correctly imported.","error":"TypeError: (0 , _linqToTypescript.from) is not a function"},{"fix":"Ensure your `tsconfig.json` includes `\"target\": \"es2022\"` and `\"lib\": [\"es2022\"]`. Also, verify that `moduleResolution` is set appropriately, e.g., `\"nodenext\"` or `\"bundler\"` for modern Node.js environments.","cause":"Incorrect `tsconfig.json` configuration, particularly `target` and `lib` settings not matching the package's requirements, or issues with module resolution.","error":"TS2307: Cannot find module 'linq-to-typescript' or its corresponding type declarations."},{"fix":"Ensure that `await` is used before calling methods on `IAsyncEnumerable` or `IParallelEnumerable`. If using native extensions, confirm `initializeLinq()` has been called globally before methods are invoked.","cause":"Often happens when attempting to call an asynchronous LINQ method (e.g., `selectAsync`, `asParallel`) without correctly awaiting the `fromAsync` wrapper or the preceding async operation, or if `initializeLinq` for native types isn't called.","error":"UnhandledPromiseRejectionWarning: TypeError: [some method] is not a function"}],"ecosystem":"npm"}