dart-compared-to-typescript
dart-compared-to-typescript copied to clipboard
Examples for Dart code compared to TypeScript code
Dart compared to TypeScript
Examples for Dart code compared to TypeScript code.
You can easily try all Dart examples on https://dartpad.dartlang.org/ and TypeScript examples on http://www.typescriptlang.org/play/
Packages
TypeScript
package.json // NPM
Dart
pubspec.yaml // Pub
Comments
TypeScript
// single line comment
/*
multi line comment
*/
Dart
// single line comment
//
// multi line comment
//
/// documentation comment
Logging
TypeScript
console.log('TypeScript code');
Dart
print('Dart code');
Basic types
TypeScript
const boolType: boolean = true;
const numberType: number = 5;
const stringType: string = 'John';
const arrayType: [] = [];
const anyType: any = 'John'; // Can be anything;
const tupleType: [string, number] = ['John', 5];
Dart
final bool boolType = true;
final int numberType = 5;
final double doubleType = 5.5;
final String stringType = 'John';
final List listType = [];
final dynamic dynamicType = 'John'; // Can be anything;
Variables
TypeScript
var a: string = 'a';
let b: string = 'b';
const c: string = 'c';
Dart
var a = 'a';
-
final String c = 'c'; // runtime constant
const c = 'c'; // compile time constant / freezing
Interpolation
TypeScript
const firstName: string = 'John';
const lastName: string = 'Doe';
console.log(`This is ${firstName} ${lastName})
Dart
final String firstName = 'John';
final String lastName = 'Doe';
print('This is $firstName $lastName');
Arrays
TypeScript
const persons: string[] = ['John', 'William'];
Dart
final List<String> persons = ['John', 'William'];
Objects
TypeScript
const person = {
name: 'John',
};
console.log(person.name) // John
Dart
// As a map
Map person = {
'name': 'John'
}
print(person['name']) // John
// As a class
class Person {
String name = 'John';
}
var person = Person();
print(person.name) // John
Functions
TypeScript
function add(a: number, b: number): number {
return a + b;
}
Dart
int add(int a, int b) {
return a + b;
}
Async functions
TypeScript
async function add(a: number, b: number): number {
return a + b;
}
Dart
Future<int> add(int a, int b) async {
return a + b;
}
Classes
TypeScript
export class Foo {}
Dart/Flutter
class Foo extends StatelessWidget {}
class Foo extends StatefulWidget {}
Constructor
TypeScript
export class Foo {
person: string;
constructor(name) {
this.person = name;
}
}
Dart/Flutter
class Foo {
String person;
Foo({ this.person })
}
Overwriting classes
TypeScript
export class Foo {
constructor() {
super()
}
}
Dart/Flutter
class Foo {
Foo({Key key}) : super(key:key);
}
Working with JSON
TypeScript
const person = {
name: "John Doe"
}
// stringify
const personJson = JSON.stringify(person);
console.log(personJson); // '{ "name": "John Doe", "age": 33 }'
// parse
const personObject = JSON.parse('{ "name": "John Doe", "age": 33 }');
console.log(personObject.name) // John Doe
Dart/Flutter
import 'dart:convert';
Map person = {
"name": "John Doe"
};
// stringify
var personJson = jsonEncode(person);
print(personJson); // '{ "name": "John Doe", "age": 33 }'
// parse
Map personMap = jsonDecode('{ "name": "John Doe", "age": 33 }');
print(personMap["name"]) // John Doe