简体   繁体   English

Typescript 返回类型 function 的自动完成

[英]Typescript autocomplete for return type of function

I am trying to create a simply function what would return value of object by key and, if it is a function, it would execute it and return correct type, so it can be autocompleted by code editor.我正在尝试创建一个简单的 function,它将按键返回 object 的值,如果它是 function,它将执行它并返回正确的类型,因此它可以由代码编辑器自动完成。

type defaults = {
  a: "A" | "B" | (() => "A" | "B");
  b: "X" | "Y" | (() => "X" | "Y");
};

const myOpts: defaults = {
  a: () => "A",
  b: () => "X",
};

function option(name: string) {
  if (!myOpts[name]) {
    return null;
  }

  return typeof myOpts[name] === "function"
    ? myOpts[name].call()
    : myOpts[name];
}

console.log(myOpts.a);      // Autocomplete works
console.log(option("a"));   // Autocomplete does not work at all
console.log(option("xyz")); // Should warn that it does not exist!!!

So, is it possible to tell that the return type of the function is the type of value that is found by key in some object.那么,是否可以判断出 function 的返回类型是在某些 object 中通过 key 找到的值的类型。

So, I'm assuming you just want to have auto completion for anything that can be deduced from the type, in that case, you could just use the keyof operator所以,我假设你只想自动完成任何可以从类型中推导出来的东西,在这种情况下,你可以只使用keyof 运算符

In your scenario, you would change the option function to:在您的场景中,您会将option function 更改为:

function option(name: keyof defaults): 'A' |'B' | 'X' | 'Y' | null {
  const prop = myOpts[name];
  if (!prop) {
    return null;
  }

  if (typeof prop === 'function') {
    return prop();
  }
  return prop;
}

Which would give you a warning, if you try to use an unknown key as an argument.如果您尝试使用未知密钥作为参数,这会给您一个警告。

Now the last change that was needed, was to change how the potential function would be called.现在需要的最后一个更改是更改潜在的 function 的调用方式。 You could use prop.call( null ) here as well, but why not directly call it instead (if you are anyhow sure it's a function as checked before)您也可以在这里使用prop.call( null ) ,但为什么不直接调用它(如果您确定它是之前检查过的 function )

You can try it in the playground here你可以在这里的操场上试试

As a remark, based on the type inference, the current function wouldn't actually return null;作为评论,基于类型推断,当前的 function 实际上不会return null; anywhere now (it will always be any of the non-nullable properties) but I added the null function result to not have to change to much of your code:)现在在任何地方(它将始终是任何不可为空的属性)但我添加了 null function 结果而不必更改大部分代码:)

Another option of changing your code would be to use some more generic option, say:更改代码的另一种选择是使用一些更通用的选项,例如:

function option<T>( source: T, name: keyof T): any {
  const prop = source[name];
  if (!prop) {
    return null;
  }

  if (typeof prop === 'function') {
    return prop();
  }
  return prop;
}

which would still give you validation, but the result type will probably need to be any at that time, or string if you always intend to return string values from it这仍然会给你验证,但结果类型可能需要是 any ,或者 string 如果你总是打算从它返回字符串值

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

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