arogozine/LinqToTypeScript

The library is great but does not work with React. Please Fix !!!

code-warrior-one opened this issue · 3 comments

Hi,

An overwhelmingly large proportion of React apps are created with CRA (Create React App). This lib looks very promising as it generator based and can perform LINQ on Typescript without any additional boilerplate code.

We have tried this on a few different machines with CRA. It builds, but at runtime throws the following error (using the steps from your readme) on InitializeLinq():

TypeError: Object(...) is not a function
Module../src/common/utils/Linq.ts
{My_React_App_Directory }/src/common/utils/Linq.ts:28
25 | interface String extends IEnumerable { }
26 | }
27 | // 2. Bind Linq Functions to Array and Map

28 | initializeLinq();

Please test and fix with CRA.

Thanks in advance.

Looking into this for next release.

@snapfin-git just tried the following with 6.0.0 BETA release,

Run create react app, then in the index.tsx add the following to the top,

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
// 0. Import Module
import { initializeLinq, IEnumerable, ArrayEnumerable } from "linq-to-typescript"
// 1. Declare that the JS types implement the IEnumerable interface
declare global {
    interface Array<T> extends IEnumerable<T> {
        concat(items: IEnumerable<T>): IEnumerable<T>;
        concat(...items: Array<ReadonlyArray<T>>): ArrayEnumerable<T>;
        concat(...items: Array<T | ReadonlyArray<T>>): ArrayEnumerable<T>;    
    }
    interface Uint8Array extends IEnumerable<number> { }
    interface Uint8ClampedArray extends IEnumerable<number> { }
    interface Uint16Array extends IEnumerable<number> { }
    interface Uint32Array extends IEnumerable<number> { }
    interface Int8Array extends IEnumerable<number> { }
    interface Int16Array extends IEnumerable<number> { }
    interface Int32Array extends IEnumerable<number> { }
    interface Float32Array extends IEnumerable<number> { }
    interface Float64Array extends IEnumerable<number> { }
    interface Map<K, V> extends IEnumerable<[K, V]> { }
    interface Set<T> extends IEnumerable<T> { }
    interface String extends IEnumerable<string> { }
}
// 2. Bind Linq Functions to Array and Map
initializeLinq()
// 3. Use without a wrapper type
const evenNumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9].where((x) => x % 2 === 0).toArray()
console.log(evenNumbers);

Leave the rest the same.

Running the app results in the following in console,
image

works thanks!