0

I'm writing a constructor that takes a string, and int and a function (and possibly something more that I'm not aware of at the moment). This far, I've got the following.

export class Blobb {
  constructor(public value: number, 
              public name: string, 
              public mapping: function,
              public misc: any) { ... }
}

Apparently, the variable mapping crashes the transpilation because function isn't a valid type. I'm not sure what to do with it.

Is there a specific type for a function being passed to the constructor? Is any the preferred approach? Should I consider declaring my own type?

If it's of any significance, the function to be passed will always be something like this (but with varying computations, of course).

mapping() {
  this.value * 13 + ": " + this.name;
}
  • http://stackoverflow.com/questions/20310369/declare-a-delegate-type-in-typescript –  May 08 '17 at 13:09

4 Answers4

2

You can annotate the parameter with the exact function signature instead of specifying it just as a Function. In your case it can be typed as () => void:

export class Blobb {
  constructor(public value: number, 
              public name: string, 
              public mapping: () => void,
              public misc: any) { ... }
}

TypeScript function types: https://www.typescriptlang.org/docs/handbook/functions.html#function-types

Saravana
  • 37,852
  • 18
  • 100
  • 108
  • Is it equivalent to the suggestion by @kennethchau above setting capital "f"? –  May 08 '17 at 16:13
  • For your particular example (no parameters, no return type) it is probably the same. But if you have parameters or return types you can define them the same way as above (ex: `mapping: (param: number) => number`). Which you can't do with just typing it as `Function`. – Saravana May 08 '17 at 16:16
  • I like that approach: +1. In fact, I just changed my generic `Function` to `(input: number)=>string` and I like it. The only thing I noticed that by going from `any` to `number`, I locked out e.g. `Date`. Is there a way to state that the input is *either Date or number but nothing else*? – Konrad Viltersten May 09 '17 at 13:48
  • You can use [union types](https://www.typescriptlang.org/docs/handbook/advanced-types.html#union-types): `(input: number | Date) => string` – Saravana May 09 '17 at 13:54
1

I have not used them myself but from the documents, I see that Type Aliases are a thing (Introduced in TS 1.4);

e.g.:

type NameResolver = () => string;
function getName(n: NameOrResolver): Name {...}

Typescript Advanced Types

Nico Van Belle
  • 4,911
  • 4
  • 32
  • 49
  • True, and Type Aliases are a preferable syntactic form for function types but you can also use an interface with a `()` member if you need overloads. – Aluan Haddad May 09 '17 at 03:13
0

You could use the interface 'Function' in es2015 or above, so it could look like,

export class Blobb {
  constructor(public value: number, 
              public name: string, 
              public mapping: Function,
              public misc: any) { ... }
}
CharithW
  • 61
  • 7
0

I think you need Function with an uppercase 'F' instead of function.

function is the key word to declare a new function I believe.