arogozine / LinqToTypeScript

LINQ to TypeScript
https://arogozine.github.io/linqtotypescript/
MIT License
139 stars 18 forks source link

LINQ .concat conflicts with native Typescript / Javascript .concat method #7

Closed capnkts closed 3 years ago

capnkts commented 3 years ago

We just installed your linq-to-typescript package in our Angular application and we found that it overrides the native .concat method for Typescript / JavaScript. Is there any possibility you can either rename the LINQ .concat method to something other than .concat? If not, is there any way to set up the package to not override the native .concat method?

Another great option is if the LINQ methods are being extended to the specific object, when it's a string, it would help to return a string instead of an IEnumerable. The native .concat method for strings just takes two strings and returns a concatenated string. The linq-to-typescript package will return an IEnumerable, which breaks a lot of other packages that use the native .concat. That's the reason for even bothering you with this. Thank you!

arogozine commented 3 years ago

I suggest not using initializeLinq and instead importing and using from - it will not pollute/override anything.

See the with wrappers section, https://github.com/arogozine/LinqToTypeScript#with-wrappers

I will rename the conflicting method in the next release.

capnkts commented 3 years ago

We liked your package since we didn't have to use wrappers and this is the only conflict with any other native method that we've seen. When will the next release come out? If you ever used the original LINQ-to-JavaScript in AngularJS, the LINQ methods were extended to any JavaScript array without any wrappers. So that's why we really liked your LINQ-to-Typescript package since you could do the same thing with the initializeLinq function. Renaming the conflicting method would make this package perfect.

Thanks so much for your response!

arogozine commented 3 years ago

Per, https://github.com/arogozine/LinqToTypeScript/blob/master/src/initializer/bindArray.ts I might not be overriding the method at all. I don't recall how that code. Weird.

Try the following,

const test = [1, 2].concat([1, 2])
console.log(test instanceof Array)

If that returns true to you, try the following,

declare global {
    interface Array<T> extends IEnumerable<T> {
        concat(...items: Array<ReadonlyArray<T>>): ArrayEnumerable<T>;
        concat(...items: Array<T | ReadonlyArray<T>>): ArrayEnumerable<T>;    
        concat(items: IEnumerable<T>): IEnumerable<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> { }
}

Note that the concat order has moved around.

capnkts commented 3 years ago

I did try this, and the test returned true, so then I changed the declaration of 'global' to what you specified in your previous response. The issue is that a normal Typescript .concat returns a BasicEnumerable when it should just return a string if we're concatenating two strings together.

I've attached a few files so you can see what I'm seeing. The first is the error, and it only relates to the linq-to-typescript .concat method from what I can tell: Console error for  concat conflict with linq-to-typescript  concat method

The second is the code example you had along with a normal string concatenation: Code test example

The third is the screenshot of the results of that test: Code test results

The fourth is the description of the native Typescript .concat method: Typescript concat

If you could rename it to something other than a native method in Typescript, I think that would fix the problem and allow the native .concat method to return a string when two strings are concatenated together. Right now, the LINQ .concat method returns a BasicEnumerable for two strings instead of just returning a concatenated string.

Thanks!

arogozine commented 3 years ago

I renamed the method from concat to concatenate, see if it works for you, LINQ to TypeScript 8.0 BETA

Update your globals like so

interface Array<T> extends IEnumerable<T> { }
capnkts commented 3 years ago

No errors after installing the beta version, and it does return the native .concat method string as expected now:
image

Thank you so much for your help!!

capnkts commented 3 years ago

BTW, if you'll let us know how to donate to you for the hard work on this package, please let us know.