st_clair_clarke
st_clair_clarke

Reputation: 5715

Converting TypeScript to Dart

I am trying to understand some typescript but mostly works with Dart.

I see the following example code relevant to what I am doing

import {Component} from 'angular2/core';
import {Validators, MaxLengthValidator, Control, ControlGroup} from 'angular2/common';
import {isPresent} from 'angular2/src/facade/lang';
import {bootstrap} from 'angular2/platform/browser';

export class CustomValidators {

  static minLengthWithDescription(minLength: number, desc: string): Function {
    return (control: modelModule.Control): {[key: string]: any} => {
      if (isPresent(Validators.required(control))) return null;
      var v: string = control.value;
      return v.length < minLength ?
                 {"minlength": {"requiredLength": minLength, "actualLength": v.length, "desc": desc}} :
                 null;
    };
  }
}

I can follow most of the code but what is the following really doing

return (control: modelModule.Control): {[key: string]: any} =>

Could someone who understands both language convert this small class to Dart?

Thanks

Upvotes: 3

Views: 2727

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 658027

It's mostly about moving types from right to left.
I guess the confusing part is {[key: string]: any} which I think is also just the return type of the returned function. I guess it translates to Map<String,dynamic> but there is currently no way to add a return type annotation for a closure in Dart anyway. A workaround would be to create a typedef

typedef Map<String,dynamic> SomeFunc(modelModule.Control control);

class CustomValidators {

  static SomeFunc minLengthWithDescription(int minLength, String desc) {
    return (modelModule.Control control) {
      if (isPresent(Validators.required(control))) return null;
      String v = control.value;
      return v.length < minLength ?
                 {"minlength": {"requiredLength": minLength, "actualLength": v.length, "desc": desc}} :
                 null;
    };
  }
}

I can't derive what modelModule. is from the code you provided, but I guess it is some namespace or nested scope to refer to the class Control.

Upvotes: 2

Related Questions