Skip to content

Instantly share code, notes, and snippets.

@briandk
Last active July 19, 2018 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briandk/63c9dfa434c8d123312e7978e7acd3c6 to your computer and use it in GitHub Desktop.
Save briandk/63c9dfa434c8d123312e7978e7acd3c6 to your computer and use it in GitHub Desktop.
Example functions (and arrow functions) in Typescript with target `es2017`

Defining an Object Type

const myObject: { [key: string]: string } = {foo: 'bar'}

Annotating Function Types

With target=es2017 and TypeScript 2.8

const foo = () => {
  return [1, 2, 3];
};

export const myAdd: (x: number, y: number) => number = (
  x: number,
  y: number,
) => {
  return x + y;
};

export function myAdd2(x: number, y: number): number {
  return x + y;
}

produces this JavaScript output:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const foo = () => {
    return [1, 2, 3];
};
exports.myAdd = (x, y) => {
    return x + y;
};
function myAdd2(x, y) {
    return x + y;
}
exports.myAdd2 = myAdd2;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment