Use declaration merging to build a type (Typescript)

Jeanluca Scaljeri

I have used declaration merging as follows

function barfoo(): void { /* magic */ }
namespace barfoo {
    export let maz: Array<string> = [];
}

But the question is, how can I use this. For example, if I use it as follows

function doIt(cb: barfoo): void {
    cb.maz = [10];
    cb();
}

typescript is complaining: Cannot find name 'barfoo'.

DEMO

Any suggestions how to fix the type (without using any :) ?

UPDATE: I figured out how to create new functions of this type

let x: { (): Array<string>; maz: Array<string>; };

x = (() => {
    var _x : any = function () { };
    _x.maz = [];
    return _x;
})();

let y: { (): void; maz: Array<string> } = (() => {
    let _y: any = function fake(): Array<string> {
        return ((cb: any): Array<string> => {
            return cb.maz;
        })(fake);
    }
    _y.maz = [];
    return _y;
})();

DEMO

But, as you can see, the second example is very complex. Is this the correct way or can this be simplified?

jcalz

Your problem is that barfoo is a value, but in your definition of doIt() you are mistakenly treating it as a type. All values have a type, but the name of a value is rarely the name of its type. If I do let x = 3; then x is a value, but its type is number, not x.

It seems from your code you want to be able to pass in the value barfoo to the function. (Why you would want to do such a thing is your business, but if you're only ever going to pass that one value into the function, you might want to consider not having it be a parameter at all. Up to you, though.) So the cb parameter should be declared to be the type of barfoo, whatever it is. By inspection you can see it is something like { (): void; maz: string[] }. But luckily TypeScript allows you to quickly get the type of a named value by using a type query, which reuses the keyword typeof. The type of barfoo is simply typeof barfoo:

function doIt(cb: typeof barfoo): void {
    cb.maz = [10];  // error, hey 10 is not a string
    cb();
}

This works, and even catches another bug for you. Hope that helps; good luck!


UPDATE

If you want to make more objects of the same type as barfoo, that is: a callable function (of no arguments and returning void) which also has a maz property containing an array of strings, the most straightforward way to do it is to use Object.assign(), which merges all properties into the first argument and returns it as the intersection of all the argument types. Like this:

const barfoo2 = Object.assign(
  function() {
    // function body here
  },
  {maz: ["some","stuff"]}
);

doIt(barfoo2); // works

Good luck again!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Typescript declaration merging: override?

From Dev

TypeScript declaration merging for consumers of module

From Dev

What is the use case for declaration merging?

From Dev

TypeScript type declaration

From Dev

Typescript: Specify type in object declaration

From Dev

Typescript / Angular Type declaration useless?

From Dev

Merging TypeScript interface declaration for Socket.IO is not working

From Java

TypeScript type declaration for "member should not exist on this type"?

From Dev

class declaration - Property does not exist on type in TypeScript

From Dev

Use type synonym in type declaration mapping

From Dev

How to use the Type class as type in variable declaration

From Dev

Use type synonym in type declaration mapping

From Dev

Forward declaration of class / Invalid use of incomplete type

From Dev

Invalid use of incomplete type and forward declaration

From Dev

Forward declaration of class / Invalid use of incomplete type

From Dev

Possible to use anonymous type members during declaration?

From Dev

Get Class Type of Object and use it in a Variable Declaration

From Dev

Build a Node package using Typescript 1.5 and generate the declaration file

From Dev

Declaration merging and aliasing not working together

From Dev

Is this a type declaration?

From Dev

How do I use a class defined in a TypeScript declaration file?

From Dev

Can`t extend the JavaScript native method declaration when use TypeScript

From Dev

How to build a Typescript type from an array of strings?

From Dev

Use TypeScript Type support with Underscore

From Dev

How can I use a type synonym in an instance declaration?

From Dev

use both shebang and strict type declaration in PHP 7

From Dev

Package specification and package body issue , illegal use of type before declaration

From Dev

How can I use a type synonym in an instance declaration?

From Dev

Package specification and package body issue , illegal use of type before declaration

Related Related

  1. 1

    Typescript declaration merging: override?

  2. 2

    TypeScript declaration merging for consumers of module

  3. 3

    What is the use case for declaration merging?

  4. 4

    TypeScript type declaration

  5. 5

    Typescript: Specify type in object declaration

  6. 6

    Typescript / Angular Type declaration useless?

  7. 7

    Merging TypeScript interface declaration for Socket.IO is not working

  8. 8

    TypeScript type declaration for "member should not exist on this type"?

  9. 9

    class declaration - Property does not exist on type in TypeScript

  10. 10

    Use type synonym in type declaration mapping

  11. 11

    How to use the Type class as type in variable declaration

  12. 12

    Use type synonym in type declaration mapping

  13. 13

    Forward declaration of class / Invalid use of incomplete type

  14. 14

    Invalid use of incomplete type and forward declaration

  15. 15

    Forward declaration of class / Invalid use of incomplete type

  16. 16

    Possible to use anonymous type members during declaration?

  17. 17

    Get Class Type of Object and use it in a Variable Declaration

  18. 18

    Build a Node package using Typescript 1.5 and generate the declaration file

  19. 19

    Declaration merging and aliasing not working together

  20. 20

    Is this a type declaration?

  21. 21

    How do I use a class defined in a TypeScript declaration file?

  22. 22

    Can`t extend the JavaScript native method declaration when use TypeScript

  23. 23

    How to build a Typescript type from an array of strings?

  24. 24

    Use TypeScript Type support with Underscore

  25. 25

    How can I use a type synonym in an instance declaration?

  26. 26

    use both shebang and strict type declaration in PHP 7

  27. 27

    Package specification and package body issue , illegal use of type before declaration

  28. 28

    How can I use a type synonym in an instance declaration?

  29. 29

    Package specification and package body issue , illegal use of type before declaration

HotTag

Archive