How to specify that a function returns a certain type when new'ed in TypeScript?

yankee

Suppose I have the following third party JavaScript function that I want to call from TypeScript:

function foo(obj) {
  var x = new obj.newable();
  x.bar();
}

Here is what my JavaScript code (which I want to port to typescript) does:

foo({
  newable: function() {
    this.bar = function() {
      console.log("Hi");
    }
  }
});

This successfully prints "hi". But now I want to port my code to TypeScript. Especially I want to make sure that the object returned by new obj.newable() contains a method bar.

I tried some things like:

interface IForFoo {
  newable: {new () : IWithBar};
}
interface IWithBar {
  bar: () => void;
}
function foo(obj: IForFoo) { // this will eventually be specified in a .t.ts file
  var x = new obj.newable();
  x.bar();
}

but that does not work, because Type 'new() => IWithBar' requires a construct signature, but type '() => void' lacks one. (To be honest I haven't figured out what exactly a construct signature refers to).

How do I get this "right"?

basarat

First lets get it to work.

Working example

The following works and is the simplest solution

interface Foo{
    newable:{
        () : {bar:Function}
    }
}

declare function foo(arg:Foo);

foo({
  newable: function() {
    this.bar = function() {
      console.log("Hi");
    }
    return this;
  }
});

Telling TypeScript its newable

If you want to enforce the newable constraint i.e it MUST accomodate for new you need to use a TypeScript class i.e.

interface Foo {
    newable: {
        new (): { bar: Function }
    }
}

declare function foo(arg: Foo);


class NewAble {
    bar = () => console.log('Hi');
}

foo({
    newable: NewAble
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How can I specify the function type in my type hints?

From Java

Specify return type in TypeScript arrow function

From Dev

How do I avoid type errors when internal function returns 'Union' that could be 'None'?

From Dev

How can I specify a return type for a $http call with Typescript?

From Dev

How to declare the type of a function that returns an array in Fortran?

From Dev

How to create a function that returns a generic protocol type?

From Dev

How to Specify Superclass when Creating New Class

From Dev

When to specify certain Setups in Moq

From Dev

How to make a function which returns Type <T>

From Dev

How to specify type when using ConcurrentHashMap in clojure

From Dev

How to write a function that returns something of the type `=> Int`?

From Dev

Explicitly specify generic type constraint when calling function

From Dev

How to specify a generic type for a function parameter

From Dev

How to extract the type of a function in typescript?

From Dev

When using the convert function of the watson-developer-cloud Node.js library, how do I specify the mime type of the original document?

From Dev

Typescript: Specify type in object declaration

From Dev

How to create new instance of type in Typescript?

From Dev

How to dynamically specify type in TypeScript generics?

From Dev

Why isn't Typescript requiring my function to return a certain type?

From Dev

Typescript: function that returns a predefined union type

From Dev

How to specify type of index of enum in typescript

From Dev

how to specify a c++ function pointer that may accept boost::bind'ed functions

From Dev

When to specify certain Setups in Moq

From Dev

How to make a function which returns Type <T>

From Dev

Explicitly specify generic type constraint when calling function

From Dev

Typescript function returns NaN value but number type

From Dev

When writing a comment header for documentation, how do I specify that this function returns another function?

From Dev

TypeScript Specify Type For Individual Function Use

From Dev

How to specify Typescript Mongoose model type in await

Related Related

  1. 1

    How can I specify the function type in my type hints?

  2. 2

    Specify return type in TypeScript arrow function

  3. 3

    How do I avoid type errors when internal function returns 'Union' that could be 'None'?

  4. 4

    How can I specify a return type for a $http call with Typescript?

  5. 5

    How to declare the type of a function that returns an array in Fortran?

  6. 6

    How to create a function that returns a generic protocol type?

  7. 7

    How to Specify Superclass when Creating New Class

  8. 8

    When to specify certain Setups in Moq

  9. 9

    How to make a function which returns Type <T>

  10. 10

    How to specify type when using ConcurrentHashMap in clojure

  11. 11

    How to write a function that returns something of the type `=> Int`?

  12. 12

    Explicitly specify generic type constraint when calling function

  13. 13

    How to specify a generic type for a function parameter

  14. 14

    How to extract the type of a function in typescript?

  15. 15

    When using the convert function of the watson-developer-cloud Node.js library, how do I specify the mime type of the original document?

  16. 16

    Typescript: Specify type in object declaration

  17. 17

    How to create new instance of type in Typescript?

  18. 18

    How to dynamically specify type in TypeScript generics?

  19. 19

    Why isn't Typescript requiring my function to return a certain type?

  20. 20

    Typescript: function that returns a predefined union type

  21. 21

    How to specify type of index of enum in typescript

  22. 22

    how to specify a c++ function pointer that may accept boost::bind'ed functions

  23. 23

    When to specify certain Setups in Moq

  24. 24

    How to make a function which returns Type <T>

  25. 25

    Explicitly specify generic type constraint when calling function

  26. 26

    Typescript function returns NaN value but number type

  27. 27

    When writing a comment header for documentation, how do I specify that this function returns another function?

  28. 28

    TypeScript Specify Type For Individual Function Use

  29. 29

    How to specify Typescript Mongoose model type in await

HotTag

Archive