简体   繁体   English

如何将 TypeScript 定义文件用于另一个 function

[英]How to use a TypeScript definition file for another function

I'm new to TS and would like to use a definition file I created to tell a separate function about it's arguments type.我是 TS 新手,想使用我创建的定义文件来告诉单独的 function 它是 arguments 类型。

functions.ts函数.ts

export default ({}) => {
  const myFunction = ({ param1 }: { param1: string }) => {
    return param1;
  };

  return {
    myFunction
  };
};

functions.d.ts函数.d.ts

declare var _default: ({}: {}) => {
    myFunction: ({ param1 }: {
        param1: string;
    }) => string;
};
export default _default;

someotherfile.ts其他文件.ts

export default ({ functions }) => {
  functions().myFunction({ param1: 1 });
};

The functions parameter is from functions.ts above, but of course someotherfile.ts does not know what the structure of functions is. functions参数来自上面的functions.ts,但是someotherfile.ts当然不知道functions的结构是什么。

How can I use functions.d.ts here?我怎样才能在这里使用functions.d.ts? Do I even need to use it?我什至需要使用它吗?

I think this will solve your problem, just create a new type if you need only it.我认为这将解决您的问题,如果您只需要它,只需创建一个新类型。

// functionsType.ts
export type functions = ({}: {}) => {
  myFunction: ({ param1 }: {
      param1: string;
  }) => string;
};

and use it like so并像这样使用它

//someotherfile.ts
import { functions } from './functionsType';

export default (functions: functions) => {
  functions({}).myFunction({ param1: 1 });
};

here is stackBlitz example where you can see an error about wrong argument type这是stackBlitz示例,您可以在其中看到有关错误参数类型的错误

It does not work like that.它不是那样工作的。

You use definitions to provide types to untyped javascript code you may import.您使用定义为您可能导入的无类型 javascript代码提供类型。 For instance, let's say you did import library X into yout project and it did not have a @types/X package.例如,假设您确实将库X导入到您的项目中,但它没有@types/X package。 Then you could write a .d.ts file to integrate it into your project.然后您可以编写一个.d.ts文件将其集成到您的项目中。 But that is completely optional, you can also consume JS code as-is, without types.但这完全是可选的,您也可以按原样使用 JS 代码,无需类型。

If you want the return of a function to depend on its arguments, what you are probably looking for is generics , which may be a somewhat complicated topic.如果您希望 function 的返回依赖于它的 arguments,那么您可能正在寻找的是generics ,这可能是一个有点复杂的话题。 For instance:例如:

const myFunction = <Input extends { param1: any }>(inp: Input) => {
    return inp.param1
};

On the previous excerpt we tell that myFunction accepts any object that at least has a property named param1 .在前面的摘录中,我们告诉 myFunction 接受任何 object 至少具有名为 param1 的属性 And its return value will be inferred to whatever type param1 has.它的返回值将被推断为 param1 具有的任何类型。 Which is different from writing:这与写作不同:

const myFunction = (inp: { param1: any, [k: string]: any }) => {
    return inp.param1
};

This last example puts similar constraints on the function inputs, but the return type will be always erased to any .最后一个示例对 function 输入施加了类似的约束,但返回类型将始终擦除为any

The difference is that, while in the 2nd sample we are "casting" the input type, on the 1st sample, we are generically using whichever type comes in while imposing a constraint.不同之处在于,在第二个示例中,我们“强制转换”输入类型,而在第一个示例中,我们通常在施加约束时使用进入的任何类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM