-
Notifications
You must be signed in to change notification settings - Fork 62
Builtin façade methods
ts2dart supports automatically translating "façade" methods, i.e. translating certain methods of well known types from JavaScript/TypeScript into their Dart equivalents. This mode is configurable and enabled using the option translateBuiltins.
Support is experimental - expect bugs.
You can find the supported, builtin façade methods in facade_converter.ts.
Currently supported:
-
Array.push==>List.add -
Array.map==>List.map(...).toList() -
Array.concat==>new List.from(...)..addAll(...) -
Array.isArray==>... is List -
Map.get(k)==>m[k] -
Map.set(k, v)==>m[k] = v; -
Map.has(k)==>m.contains(k) -
/regexp/==>new RegExp(\r'regexp') -
RegExp.test==>RegExp.hasMatch -
RegExp.exec==>RegExp.allMatches(...).toList() -
CONST_EXPR,forwardRefas we had before, but with more precision.
Meaning you should be able to just write:
var xs: string[] = [];
xs.push('12');
var sum = 0;
xs.map((x) => parseInt(x)).forEach((x) => sum += x);And for Maps:
var m = new Map<string, number>();
m.set('answer', 42);
console.log(m.get('answer'));To handle builtins, ts2dart has to know which calls to e.g. get are on Map, and which are on some unrelated object and should not be translated. If your code contains a get call on an unknown type (any in TypeScript), ts2dart will give an error like this:
[16:39:36] TS2DartError: [TSToDartTranspiler]: angular2/my/file.ts:79:37: Untyped property access to "get" which could be a special ts2dart builtin. Please add type declarations to disambiguate.
Your code probably looks somewhat like this:
var x;
// ... some code, maybe setting `x` to a value ...
var sth = x.get('something');To fix the problem, add a type declaration to x, so that the expression that you call get on has a known, specific type:
var x: Injector;
// ... rest of the code is unchanged
The same applies to push, map, and so on.