electricessence / TypeScript.NET

A JavaScript-Friendly .NET Based TypeScript Library (Moved)
https://github.com/electricessence/TypeScript.NET-Core
Other
251 stars 36 forks source link

Uncaught (in promise): There was a problem importing System.Linq/Linq #72

Closed aammfe closed 5 years ago

aammfe commented 5 years ago

Uncaught (in promise): There was a problem importing System.Linq/Linq

image

`import { List } from 'typescript-dotnet-commonjs/System/Collections/List';

public anyRoleMatch(allwedRoles: string[]) { if (this.authService.isLoggedIn === false) { return false; }

const list = new List<string>(allwedRoles);
list.linq.forEach(item => {
  console.log(item);
});
// const userRoles = Enumerable.from(AuthService.decodedToken.role);
// return userRoles.getEnumerator().intersect(Enumerable.from(allwedRoles)).any();

return false;

}`

electricessence commented 5 years ago

Are you executing this in a browser or Node environment? .linq will only work in a Node environment. (Is provided as a convenience for Node users.) Please use .linqAsync(callback) as your default implementation. https://github.com/electricessence/TypeScript.NET/blob/master/source/System/Collections/CollectionBase.ts#L461

This is because the Linq module is huge and instead of incurring that load by default in a web environment it's set up to be deferred.

If you absolutely need Linq functionality (which typically in TS/JS you don't) then you'll need to leverage Enumerable to do that.

All of the following forEach methods will produce the same results.

import List from "typescript-dotnet-umd/System/Collections/List";
import Enumerable from "typescript-dotnet-umd/System.Linq/Linq";
const userRoles = ["a","b","c"]; // just for example

console.log("Using array.forEach()");
userRoles.forEach(e=>console.log(e));

console.log("Using List.forEach() (CollectionBase.forEach)");
const list = new List<string>(userRoles);
list.forEach(e=>console.log(e));

console.log("Using Enumerable.forEach()");
const listE = Enumerable(list);
listE.forEach(e=>console.log(e));

array.forEach implementation is slightly different as it includes the source array as on of the parameters.

electricessence commented 5 years ago

In respect to your sample code, this works:

import Enumerable from "typescript-dotnet-umd/System.Linq/Linq";
const allowedRoles = ["a", "b", "c", "d"]; // just for example
const userRoles = ["a", "b", "x", "y"];

const userRolesE = Enumerable(userRoles);
const urIntersect = userRolesE.intersect(allowedRoles).any();
console.log(urIntersect); // true
electricessence commented 5 years ago

This also works:

import Set from "typescript-dotnet-umd/System/Collections/Set";
const allowedRoles = new Set<string>(["a", "b", "c", "d"]); // just for example
const userRoles = ["a", "b", "x", "y"];

console.log(allowedRoles.overlaps(userRoles)); // true

Where a Set<T> is any combination of a single primitive type.

electricessence commented 5 years ago

Enumerable(collection) is an alias for Enumerable.from(collection) and it's smart enough to know if it's an array, an enumerator, or an enumerable.

aammfe commented 5 years ago

Thanx for quick reply. and Thanx for making this lib it feels like home for .net persons basically I m from .net and its all new to me. I'm working on angular (front end) and after seeing your examples linq intersection function is working fluently.

Please give me suggestion. for static data manipulation What should I use ? Rxjs or linq .

static data means data which is not stream. like my example

electricessence commented 5 years ago

JS/TS is uses arrays as the most common collection so it's best to get familiar with the existing array methods like:

.map(selector) // aka .Select(selector)
.filter(predicate) // aka .Where(predicate)
.reduce(...) // aka .Aggregate(...)
.some(predicate) // .Any(predicate)
.every(predicate) // .All(predicate)

RxJS is incredibly well developed and adopted. If you are doing Angular or any major data binding then RxJS is probably good to learn.

You're welcome to use the Linq library but I've found that for the most part it's a transitional piece that is less needed the more you get into the language.

Learn some of the TS shortcuts like:

for(var e of entities) { } // aka foreach