TypeScript 当在先前的参数中提供对象字面量T时,未显示T键的字符串补全,

ct2axkht  于 9个月前  发布在  TypeScript
关注(0)|答案(2)|浏览(87)

Bug报告

🔎 搜索词

不正确的建议

🕗 版本和回归信息

此问题出现在v4.5.4和夜间版本中。(可能在每个版本中都存在)

⏯ Playground链接

带有相关代码的Playground链接

💻 代码

  1. type Methods = {
  2. [key: string]: (param: {
  3. apple: 'apple'
  4. }) => any
  5. }
  6. function testFunc<M extends Methods>(options: {
  7. methods: M,
  8. keys: keyof M
  9. }): void {}
  10. testFunc({
  11. methods: {
  12. myMethod: (param) => {
  13. console.log(param.apple)
  14. }
  15. },
  16. keys:
  17. })

🙁 实际行为

当将鼠标悬停在第18行的"keys"上时,它显示其类型为"myMethod"。当我开始为其值输入时,"myMethod"没有被建议。

🙂 预期行为

在第18行输入值时,应该出现"myMethod"的建议。

yzuktlbb

yzuktlbb1#

有趣的是,这起作用了

  1. type Methods = {
  2. [key: string]: (param: {
  3. apple: 'apple'
  4. }) => any
  5. }
  6. function testFunc<M extends Methods>(options: {
  7. methods: M,
  8. keys: keyof M
  9. }): void { }
  10. const obj = {
  11. myMethod: () => { },
  12. foo: () => { },
  13. };
  14. testFunc({
  15. methods: obj,
  16. keys: "" // <- here
  17. });
展开查看全部
iezvtpos

iezvtpos2#

你好!我相信这个问题并不新鲜,我过去也遇到过同样的情况。
在第18行输入值时,应该建议"myMethod"。
实际上,在你输入字符串之前,类型已经变成了string | number,所以你可以在写引号之前触发自动补全功能。

相关问题