lucasgmagalhaes / dart-compared-to-typescript

Examples for Dart code compared to TypeScript code

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Dart compared to TypeScript

Examples for Dart code compared to TypeScript code

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'];

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 {
   constructor() {}  
}

Dart/Flutter

class Foo extends StatelessWidget  {
   Foo() {}
}

About

Examples for Dart code compared to TypeScript code

License:MIT License