简体   繁体   English

Typescript Generics 不带类型

[英]Typescript Generics NOT with type

Let's say i have an array of types:假设我有一个类型数组:

const arr: Foo[]: [
  a: Foo<A>,
  b: Foo<B>,
  c: Foo<C>,
];

How to limit the array to be NOT a type如何将数组限制为非类型

const arr: Foo<Not<B>>[]: [
  a: Foo<A>,
  b: Foo<B>, // error
  c: Foo<C>,
];

One way of doing that is to use the Exclude utility type to specify a susbet of the type parameters that Foo can accept, something like this一种方法是使用Exclude实用程序类型来指定Foo可以接受的类型参数的子集,如下所示

type Foo<T> = {
    value: T
}

type Bar = string | boolean | number
type BarWithoutBoolean = Exclude<Bar, boolean>

const arr: Foo<BarWithoutBoolean>[] = [
    { value: 1     },
    { value: "abc" }, 
    { value: true  } // Err
]

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

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