Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 返回自身的复杂函数的类型脚本类型声明_Javascript_Typescript_Types - Fatal编程技术网

Javascript 返回自身的复杂函数的类型脚本类型声明

Javascript 返回自身的复杂函数的类型脚本类型声明,javascript,typescript,types,Javascript,Typescript,Types,我有以下javascript函数,它返回一个包含对象参数中给出的附加方法的函数。 我认为代码比解释更容易理解: var scopeFunction = (object) => { // create main function if it doesn't exist if (!object.main) object.main = function(){ alert("No main function for this scopeFunction"); }; //

我有以下javascript函数,它返回一个包含对象参数中给出的附加方法的函数。
我认为代码比解释更容易理解:

var scopeFunction = (object) => {

  // create main function if it doesn't exist
  if (!object.main) object.main = function(){
    alert("No main function for this scopeFunction");
  };

  // add all object keys to the main function that will be return
  _.each(object, function(d,i){ object.main[i] = d; });

  // return main function
  return object.main;

};
我想在typescript中正确地定义这段代码,这就是我所做的,但是atom typescript(这就是我测试它的地方)在我尝试访问返回函数对象的键时抛出错误。 这是我当前代码的外观:

// TYPES
namespace ScopeFunction {

  export interface Function<Obj extends MakerObject, Key extends keyof Obj>  {
    (): Obj["main"];
    [key: Key]: Obj[Key];
  }
  export interface MainFunction {
    (...args:any[]) : any;
  }
  export interface MakerObject {
    [key: string]: any;
    main?: MainFunction;
  }

  export type Maker = <Obj extends MakerObject, Key extends keyof Obj>(object:Obj) => ScopeFunction.Function<Obj, Key>;

}

// FUNC
var scopeFunction:ScopeFunction.Maker = (object) => {

  // create main function if it doesn't exist
  if (!object.main) object.main = function(){
    alert("No main function for this scopeFunction");
  };

  // add all object keys to the main function that will be return
  _.each(object, function(d,i){ object.main[i] = d; });

  // return main function
  return object.main;

};

// TEST
var test = scopeFunction({
  a: 1,
  b: "3",
  main: () => { console.log("testLog"); return 0; }
})

var test1 = test(); // WORKS OK
var test2 = test.main(); // ALERT: Property 'main' doesn't exist on type 'Function<{ a: number; b: string; main: () => number }, "main" | "a" | "b">'
var test3 = test.a; // ALERT: Property 'a' doesn't exist on type 'Function<{ a: number; b: string; main: () => number }, "main" | "a" | "b">'
//类型
命名空间作用域函数{
导出接口功能{
():Obj[“main”];
[键:键]:Obj[键];
}
导出接口主功能{
(…args:any[]):any;
}
导出接口MakerObject{
[键:字符串]:任意;
主?:主功能;
}
导出类型Maker=(对象:Obj)=>ScopeFunction.Function;
}
//FUNC
var scopeFunction:scopeFunction.Maker=(对象)=>{
//如果主函数不存在,则创建它
如果(!object.main)object.main=函数(){
警报(“此scopeFunction没有主功能”);
};
//将所有对象键添加到将返回的主函数
_.each(对象,函数(d,i){object.main[i]=d;});
//返回主函数
返回object.main;
};
//试验
var test=scopeFunction({
答:1,,
b:“3”,
main:()=>{console.log(“testLog”);返回0;}
})
var test1=test();//行吗
var test2=test.main();//警告:类型“Function number},“main”|“a”|“b”>上不存在属性“main”
var test3=test.a;//警告:类型“Function number},“main”|“a”|“b”>上不存在属性“a”

知道我的定义中的问题在哪里吗?

您的代码有几个问题:

  • 定义不编译
    [key:key]:Obj[key]
    无效,索引器参数必须是
    number
    string
    (这些类型且仅这些类型有效)。您需要改用映射类型
  • ():Obj[“main”]
    将不是与
    Obj[“main”]
    相同类型的调用签名,它将是一个返回
    main
    的任何属性的函数
  • main
    函数的类型过于泛型,它不会保留任何参数类型 实现您期望的解决方案可能是:

    namespace ScopeFunction {
    
        export type Function<Obj extends MakerObject<(...args: any[]) => any>> = Obj['main'] & {
            [P in keyof Obj]: Obj[P];
        }
        export interface MakerObject<TMain extends (...args: any[]) => any> {
            main?: TMain;
        }
    
        export type Maker = <TObj extends MakerObject<(...args: any[]) => any>>(object: TObj) => ScopeFunction.Function<TObj>;
    
    }
    
    // FUNC
    var scopeFunction: ScopeFunction.Maker = (object) => {
    
        // create main function if it doesn't exist
        if (!object.main) object.main = function () {
            alert("No main function for this scopeFunction");
        };
    
        // return main function
        return Object.assign(object.main, object);
    };
    
    
    // TEST
    var test = scopeFunction({
        a: 1,
        b: "3",
        main: (param: number) => { console.log("testLog"); return param; }
    })
    
    var test1 = test(10);
    var test2 = test.main(10); 
    var test3 = test.a;
    
    命名空间作用域函数{
    导出类型函数>=Obj['main']&{
    [P in keyof Obj]:Obj[P];
    }
    导出接口MakerObject any>{
    主?:TMain;
    }
    导出类型Maker=>(对象:TObj)=>ScopeFunction.Function;
    }
    //FUNC
    var scopeFunction:scopeFunction.Maker=(对象)=>{
    //如果主函数不存在,则创建它
    如果(!object.main)object.main=函数(){
    警报(“此scopeFunction没有主功能”);
    };
    //返回主函数
    返回Object.assign(Object.main,Object);
    };
    //试验
    var test=scopeFunction({
    答:1,,
    b:“3”,
    main:(param:number)=>{console.log(“testLog”);返回param;}
    })
    var test1=测试(10);
    var test2=test.main(10);
    var test3=test.a;