Code Monkey home page Code Monkey logo

es-type-hinting's Introduction

ECMAScript Type Hinting

This proposal introduces type hinting.

Inspired by types in Go. Similar to flow or TypeScript, but uses WSP (SP / HTAB) instead of a colon :.

Examples

// without defaults
// uses type default: Number = 0, String = "", [Object] = undefined
var point = {
  x Number,
  y Number
};

// with defaults
var point2 = {
  x Number: 10,
  y Number: 50
};

// without defaults, same as var point above
class Point {
  x Number,
  y Number
}

// with defaults, assuming properties use ':'
class Point2 {
  x Number: 0
  y Number: 0
}

// with defaults, assuming properties use '='
class Point3 {
  x Number = 0
  y Number = 0
}

function print(text String) {
  console.log(text);
}

function toLowerCase(text String) String {
  return text.toLowerCase();
}

function map(array Array, callback Function) {
  return array.map(callback);
}

Motivation and Overview

A lot of variations of type hinting exists, such as flow, TypeScript, and other proposals. These syntaxes use a colon :, similar to ActionScript. The : syntax is ambiguous to existing plain JavaScript objects:

var point = {
  x: 0,
  y: 0
};
// vs
var point = {
  x: Number,
  y: Number
};

The motivation behind this proposal is to introduce a simple way of doing type hinting without complicated syntax.

// this
var point = {
  x Number: 0,
  y Number: 0
};
// instead of (requires `= value`)
var point = {
  x: Number = 0,
  y: Number = 0
};
// or some other complex syntaxs
var point = {
  x :Number: 0,
  y :Number: 0
};
var point = {
  x :: Number : 0,
  y :: Number : 0
};

Using simple spaces translates well in other areas:

function map(array Array, callback Function) {...}

// doesn't matter if class properties end up using `:` or `=`
class Point {
  x Number,
  y Number
}
class Point2 {
  x Number: 0
  y Number: 0
}
class Point3 {
  x Number = 0
  y Number = 0
}

Syntax

TODO

es-type-hinting's People

Contributors

lukescott avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.