Typescript条件泛型类型在返回类型中

huangapple go评论106阅读模式
英文:

Typescript conditional generic type in return type

问题

  1. 如何在值不存在时对返回类型的泛型进行条件化?
  2. type Foo = {};
  3. class Bar<P extends Foo> {
  4. static make<P extends Foo>(a?: P): Bar<P> {
  5. return new Bar();
  6. }
  7. }
  8. Bar.make() // 返回 Bar<Foo>
  9. 但我需要像这样做:
  10. class Bar<P extends Foo> {
  11. static make<P extends Foo>(a?: P): Bar<P extends undefined ? never : Foo> {
  12. return new Bar();
  13. }
  14. }
  15. Bar.make() // 返回 Bar<never>
  16. Bar.make({}) // 返回 Bar<Foo>
英文:

How to condition the generic of return type if value is not present?

  1. type Foo = {};
  2. class Bar&lt;P extends Foo&gt; {
  3. static make&lt;P extends Foo&gt;(a?: P): Bar&lt;P&gt; {
  4. return new Bar();
  5. }
  6. }
  7. Bar.make() // return Bar&lt;Foo&gt;

But I need to do something like:

  1. class Bar&lt;P extends Foo&gt; {
  2. static make&lt;P extends Foo&gt;(a?: P): Bar&lt;P extends undefined ? never : Foo&gt; {
  3. return new Bar();
  4. }
  5. }
  6. Bar.make() // return Bar&lt;never&gt;
  7. Bar.make({}) // return Bar&lt;Foo&gt;

答案1

得分: 1

You need to apply a default type never.

  1. type Foo = {test: number}; // Example implementation of foo, used in test cases below
  2. class Bar<P extends Foo> {
  3. // Allow P to extend from Foo, then assign never as the default
  4. static make<P extends Foo = never>(a?: P): Bar<P> {
  5. return new Bar();
  6. }
  7. }
  8. const t1 = Bar.make(); // Bar<never>
  9. const t2 = Bar.make({ test: 2 }) // Bar<{test: 2}> (subtype of Foo)
  10. const t3: Bar<Foo> = Bar.make({ test: 2 }); // Bar<Foo> (explicit typecasting to Bar<Foo>)
  11. const t4 = Bar.make({ bazz: 0 }); // Type error, { bazz: 0 } is not assignable to Foo

Here is a TypeScript Playground link to showcase the different outcomes.

英文:

You need to apply a default type never.

  1. type Foo = {test: number}; // Example implementation of foo, used in test cases below
  2. class Bar&lt;P extends Foo&gt; {
  3. // Allow P to extend from Foo, then assign never as the default
  4. static make&lt;P extends Foo = never&gt;(a?: P): Bar&lt;P&gt; {
  5. return new Bar();
  6. }
  7. }
  8. const t1 = Bar.make(); // Bar&lt;never&gt;
  9. const t2 = Bar.make({ test: 2 }) // Bar&lt;{test: 2}&gt; (subtype of Foo)
  10. const t3: Bar&lt;Foo&gt; = Bar.make({ test: 2 }); // Bar&lt;Foo&gt; (explicit typecasting to Bar&lt;Foo&gt;)
  11. const t4 = Bar.make({ bazz: 0 }); // Type error, { bazz: 0 } is not assignable to Foo

Here a TypeScript Playground link to showcase the different outcomes.

huangapple
  • 本文由 发表于 2020年1月6日 21:22:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612912.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定