TypeScript 相关类型约束在返回类型推断下失效

bvjveswy  于 9个月前  发布在  TypeScript
关注(0)|答案(6)|浏览(134)

TypeScript 版本: 3.5.1
搜索词:

返回类型,泛型,约束,可分配,相关类型

代码

  1. type XStr = {x:string};
  2. type XNum = {x:number};
  3. type U = XStr|XNum;
  4. type Args = { str : XStr, num : XNum };
  5. declare function foo<
  6. ReturnT extends U,
  7. ValueT extends ReturnT["x"]
  8. > (
  9. f : (args : Args) => ReturnT,
  10. value : ValueT
  11. ) : void;
  12. /*
  13. Error as expected.
  14. Type 'string | number' does not satisfy the constraint 'string'.
  15. Type 'number' is not assignable to type 'string'.
  16. */
  17. foo<XStr, string|number>(
  18. (args:Args) => args.str,
  19. ""
  20. );
  21. //Inferred type, foo<XStr, string | number>
  22. foo(
  23. args => args.str,
  24. //Expected: Error
  25. //Actual: OK
  26. "" as string|number
  27. );
  28. //Inferred type, foo<XStr, string>
  29. foo(
  30. //Added explicit type annotation to function params
  31. (args:Args) => args.str,
  32. /*
  33. Error as expected.
  34. Type 'string | number' does not satisfy the constraint 'string'.
  35. Type 'number' is not assignable to type 'string'.
  36. */
  37. "" as string|number
  38. );
  39. /////
  40. /*
  41. Error as expected.
  42. Type '1' does not satisfy the constraint 'string'.
  43. */
  44. foo<XStr, 1>(
  45. (args:Args) => args.str,
  46. 1
  47. );
  48. //Inferred type, foo<XStr, 1>
  49. foo(
  50. args => args.str,
  51. //Expected: Error
  52. //Actual: OK
  53. 1
  54. );
  55. //Inferred type, foo<XStr, string>
  56. foo(
  57. //Added explicit type annotation to function params
  58. (args:Args) => args.str,
  59. /*
  60. Error as expected.
  61. Type '1' does not satisfy the constraint 'string'.
  62. */
  63. 1
  64. );

预期行为:

我只是称之为 相关类型 ,因为它让我想起了SQL中的相关子查询。

  1. ValueT 的约束类型取决于 ReturnT 的类型。
  2. f 没有参数,或者所有参数都 显式 标注时,
    ValueT 被正确推断。
  3. f 显式标注的参数时,
    ValueT 被错误地推断。
  4. 尝试显式设置无效的类型参数将按预期出错。
  • foo<XStr, string|number> 不被允许
  • foo<XStr, 1> 不被允许
    实际行为:
  • foo<XStr, string|number> 在推断下被允许
  • foo<XStr, 1> 在推断下被允许
    Playground 链接:

Playground

相关问题:

32540 (评论)

#29133
一个不同且更复杂的示例,

14829 (评论)

[编辑]
有人能想出一个更好的名字吗?
我正在重写我的类型安全的SQL构建器库,它依赖于泛型函数的返回类型被正确推断。但似乎返回类型推断在很多意想不到的地方会出错。
匿名回调函数经常用于构建 WHEREORDER BYGROUP BYHAVINGJOIN 等子句。
由于泛型函数的返回类型推断不可靠,基本上对我来说是一个障碍=()

lxkprmvk

lxkprmvk1#

从上述代码片段的行为以及由工具提示显示的推断类型来看,感觉像是有人忘记复制粘贴一些类型推断逻辑或其他东西。
例如,Inferred type, foo<XStr, 1> 代码片段的类型被推断为 foo<XStr, 1>,但它的行为却像 foo<U, 1>

tyg4sfes

tyg4sfes2#

!@#$ YES!
我终于找到了一个解决方法!

  1. type XStr = {x:string};
  2. type XNum = {x:number};
  3. type U = XStr|XNum;
  4. type Args = { str : XStr, num : XNum };
  5. declare function foo<
  6. ReturnT extends U,
  7. ValueT extends ReturnT["x"]
  8. > (
  9. ...f : (
  10. (
  11. ReturnT extends U ?
  12. [((args : Args) => ReturnT), ValueT] :
  13. never
  14. )
  15. )
  16. ) : ValueT;
  17. /*
  18. Error as expected.
  19. Type 'string | number' does not satisfy the constraint 'string'.
  20. Type 'number' is not assignable to type 'string'.
  21. */
  22. foo<XStr, string|number>(
  23. (args:Args) => args.str,
  24. ""
  25. );
  26. //Inferred type, foo<XStr, string>
  27. foo(
  28. args => args.str,
  29. /*
  30. Error as expected.
  31. Type 'string | number' does not satisfy the constraint 'string'.
  32. Type 'number' is not assignable to type 'string'.
  33. */
  34. "" as string|number
  35. );
  36. //Inferred type, foo<XStr, string>
  37. foo(
  38. //Added explicit type annotation to function params
  39. (args:Args) => args.str,
  40. /*
  41. Error as expected.
  42. Type 'string | number' does not satisfy the constraint 'string'.
  43. Type 'number' is not assignable to type 'string'.
  44. */
  45. "" as string|number
  46. );
  47. /////
  48. /*
  49. Error as expected.
  50. Type '1' does not satisfy the constraint 'string'.
  51. */
  52. foo<XStr, 1>(
  53. (args:Args) => args.str,
  54. 1
  55. );
  56. //Inferred type, foo<XStr, string>
  57. foo(
  58. args => args.str,
  59. /*
  60. Error as expected.
  61. Type '1' does not satisfy the constraint 'string'.
  62. */
  63. 1
  64. );
  65. //Inferred type, foo<XStr, string>
  66. foo(
  67. //Added explicit type annotation to function params
  68. (args:Args) => args.str,
  69. /*
  70. Error as expected.
  71. Type '1' does not satisfy the constraint 'string'.
  72. */
  73. 1
  74. );
  75. //Return type is "hello" as expected
  76. foo(
  77. args => args.str,
  78. "hello"
  79. );
  80. //Return type is 42 as expected
  81. foo(
  82. args => args.num,
  83. 42
  84. );

Playground
这里的魔法是,

  1. ...f : (
  2. (
  3. ReturnT extends U ?
  4. [((args : Args) => ReturnT), ValueT] :
  5. never
  6. )
  7. )

它将 ReturnT 转换为元组(2元组,在这个例子中),然后使用剩余参数。
[编辑]
在花了周六的大部分时间思考解决方法后,我突然在随机回想起这里的评论后想到了这个主意。

32596 (评论)

我曾经提出过使用元组和剩余参数作为解决这个问题的可能方法(在该问题中有另一种更好的表达方式)。
[编辑]
等等...我刚刚意识到这个解决方法没有使用 ValueT 。我一定是太累了才会犯这种错误。
Brb. 要修好它。(或者尝试)。
我的用例需要 ReturnTValueT ,并在返回类型中对其进行操作。
[编辑]
修复了使用 ValueT 的问题。
[编辑]
算了吧。我试图将其适应到实际的用例中,但这个解决方法崩溃了。
首先,我想让我的 ReturnT 有选择成为联合类型的权利。
所以,我不能让条件类型执行分布。
如果它不分布,我们就会回到推理问题的第一步。
Playground

展开查看全部
ldioqlga

ldioqlga3#

这更接近我的个人用例,

  1. type XStr = {x:string};
  2. type XNum = {x:number};
  3. type U = XStr|XNum;
  4. type Args = { str : XStr, num : XNum };
  5. declare function foo<
  6. ReturnT extends U,
  7. ValueT extends ReturnT["x"]
  8. > (
  9. ...f : (
  10. (
  11. //Uses `& ReturnT["x"]` to make this work
  12. [((args : Args) => ReturnT), ValueT & ReturnT["x"]]
  13. )
  14. )
  15. ) : ValueT;
  16. //OK!
  17. //Inferred type, foo<XStr | XNum, string | number>
  18. foo(
  19. args => Math.random() > 0.5 ? args.str : args.num,
  20. "" as string|number
  21. );
  22. /*
  23. Error as expected.
  24. Type 'string | number' does not satisfy the constraint 'string'.
  25. Type 'number' is not assignable to type 'string'.
  26. */
  27. foo<XStr, string|number>(
  28. (args:Args) => args.str,
  29. ""
  30. );
  31. //Inferred type, foo<XStr, string|number>
  32. foo(
  33. args => args.str,
  34. /*
  35. Expected: Error
  36. Actual: OK
  37. */
  38. "" as string|number
  39. );
  40. //Inferred type, foo<XStr, string>
  41. foo(
  42. //Added explicit type annotation to function params
  43. (args:Args) => args.str,
  44. /*
  45. Error as expected.
  46. Type 'string | number' does not satisfy the constraint 'string'.
  47. Type 'number' is not assignable to type 'string'.
  48. */
  49. "" as string|number
  50. );
  51. /////
  52. /*
  53. Error as expected.
  54. Type '1' does not satisfy the constraint 'string'.
  55. */
  56. foo<XStr, 1>(
  57. (args:Args) => args.str,
  58. 1
  59. );
  60. //Inferred type, foo<XStr, 1>
  61. foo(
  62. args => args.str,
  63. /*
  64. Expected: Error
  65. Actual: OK
  66. */
  67. 1
  68. );
  69. //Inferred type, foo<XStr, string>
  70. foo(
  71. //Added explicit type annotation to function params
  72. (args:Args) => args.str,
  73. /*
  74. Error as expected.
  75. Type '1' does not satisfy the constraint 'string'.
  76. */
  77. 1
  78. );
  79. //Return type is "hello" as expected
  80. foo(
  81. args => args.str,
  82. "hello"
  83. );
  84. //Return type is 42 as expected
  85. foo(
  86. args => args.num,
  87. 42
  88. );

Playground
我需要允许返回一个联合类型并相应地处理它。
我的实际用例需要使用 UnionToIntersection<> ,尽管如此。

展开查看全部
0wi1tuuw

0wi1tuuw4#

看起来使用UnionToIntersection<>会破坏它,
NoInfer<>也没有帮助。

  1. type UnionToIntersection<U> = (
  2. (
  3. U extends any ? (k: U) => void : never
  4. ) extends (
  5. (k: infer I) => void
  6. ) ? I : never
  7. );
  8. type XStr = {x:{propStr:string}};
  9. type XNum = {x:{propNum:number}};
  10. type U = XStr|XNum;
  11. type Args = { str : XStr, num : XNum };
  12. declare function foo<
  13. ReturnT extends U
  14. > (
  15. ...f : (
  16. (
  17. [
  18. ((args : Args) => ReturnT),
  19. UnionToIntersection<
  20. ReturnT["x"]
  21. >
  22. ]
  23. )
  24. )
  25. ) : void;
  26. foo(
  27. args => Math.random() > 0.5 ? args.str : args.num,
  28. //OK!
  29. //Error: Property 'propNum' is missing
  30. {
  31. propStr : "hello",
  32. }
  33. );
  34. foo(
  35. args => Math.random() > 0.5 ? args.str : args.num,
  36. //OK!
  37. //Error: Property 'propStr' is missing
  38. {
  39. propNum : 42,
  40. }
  41. );
  42. foo(
  43. args => Math.random() > 0.5 ? args.str : args.num,
  44. //OK!
  45. //No error
  46. {
  47. propStr : "hello",
  48. propNum : 42,
  49. }
  50. );
  51. //Expected: foo<XStr>
  52. //Actual : foo<U>
  53. foo(
  54. args => args.str,
  55. //Expected: OK
  56. //Actual : Property 'propNum' is missing
  57. { propStr : "hello" }
  58. );
  59. type NoInfer<T> = [T][T extends any ? 0 : never];
  60. declare function foo2<
  61. ReturnT extends U
  62. > (
  63. ...f : (
  64. (
  65. [
  66. ((args : Args) => ReturnT),
  67. UnionToIntersection<
  68. //NoInfer doesn't seem to work here
  69. NoInfer<ReturnT>["x"]
  70. >
  71. ]
  72. )
  73. )
  74. ) : void;
  75. //Expected: foo2<XStr>
  76. //Actual : foo2<U>
  77. foo2(
  78. args => args.str,
  79. //Expected: OK
  80. //Actual : Property 'propNum' is missing
  81. { propStr : "hello" }
  82. );

Playground
更好地重现了UnionToIntersection<>如何破坏这个解决方法,
Playground

展开查看全部
yxyvkwin

yxyvkwin5#

我忽略了除了OP之外的长篇大论。

为什么函数定义只是这个?

$x_{1a0b1}^{x}$

或者,似乎你需要一个非推断类型的参数使用操作符? $x_{1e0f1}^{x}$

zujrkrfu

zujrkrfu6#

我需要 arg 可能成为 ReturnT["x"] 的子类型。
在我的实际情况中,它用于返回类型(在我的 OP 中,返回类型为 void 以简化)。
目前,所有尝试使用非推断类型的参数用法的解决方法都在这个用例中失效了。
该函数表示 SQL 中的一列。返回类型是该列的类型。
ValueT 表示该列的可能值。
假设我们正在构建由库提供的查询,

  1. SELECT
  2. myTable.myColumn
  3. FROM
  4. myTable

假设该列的类型为 null|number
如果我们添加这个 WHERE 子句,

  1. SELECT
  2. myTable.myColumn
  3. FROM
  4. myTable
  5. WHERE
  6. myTable.myColumn <=> 1

那么我们可以在不执行查询的情况下静态缩小查询中 myColumn 的类型为 1

展开查看全部

相关问题