james.1qwrnewkbjrkwj james.1qwrnewkbjrkwj - 4 months ago 12
Dart Question

Convert simple Dart code to TypeScript

I am trying to set up Hello World, AngularJS 2 app, for the first time.

Here's simple HTML:

<!doctype html>
<html>
<body>
<my-app></my-app>
<script type="application/dart" src="hello.dart"></script>
<script src="packages/browser/dart.js"></script>
</body>
</html>


And the dart code:

class Hello{

String message;

Hello(){
HttpRequest.getString('/hello')
.then((String content) {
Map parsedMap = JSON.decode(content);
message = parsedMap["Message"];
});
}
}

main() {
reflector.reflectionCapabilities = new ReflectionCapabilities();
bootstrap(AppComponent);
}


Could anybody help me convert this Dart code to TypeScript?

Answer

Could anybody help me convert this Dart code to TypeScript?

Tips:

  • typescript has no main.
  • you need to import the stuff you need into typescript
  • has a different naming convention than dart
  • http responses of type applicaiton/json are commonly automatically parsed (objectified).

Based on that :

import {bootstrap} from "some/angular/module"; 
// Do same for http

class Hello {

  message: string;

  hello() {
        http.getString('/hello')
            .then((content) {
              const message = content;
            });
  }
}


bootstrap(AppComponent);

More

I highly recommend you read a book on TypeScript. I wrote a free one : https://basarat.gitbooks.io/typescript/content/docs/getting-started.html

Comments