Build a function object with properties in TypeScript

后端 未结 9 1310
南旧
南旧 2020-11-28 06:00

I want to create a function object, which also has some properties held on it. For example in JavaScript I would do:

var f = function() { }
f.someValue = 3;
         


        
9条回答
  •  醉话见心
    2020-11-28 06:37

    TypeScript is designed to handle this case through declaration merging:

    you may also be familiar with JavaScript practice of creating a function and then extending the function further by adding properties onto the function. TypeScript uses declaration merging to build up definitions like this in a type-safe way.

    Declaration merging lets us say that something is both a function and a namespace (internal module):

    function f() { }
    namespace f {
        export var someValue = 3;
    }
    

    This preserves typing and lets us write both f() and f.someValue. When writing a .d.ts file for existing JavaScript code, use declare:

    declare function f(): void;
    declare namespace f {
        export var someValue: number;
    }
    

    Adding properties to functions is often a confusing or unexpected pattern in TypeScript, so try to avoid it, but it can be necessary when using or converting older JS code. This is one of the only times it would be appropriate to mix internal modules (namespaces) with external.

提交回复
热议问题