简体   繁体   English

有没有一种方法可以在不创建特定类型的情况下使用具有复杂验证的断言函数?

[英]Is there a way to use assertion functions with complex validations without creating a specific type?

I'm receiving data from api and I'd like to add some validations on front-end and get correct types after that validations, but really specific ones.我正在从 api 接收数据,我想在前端添加一些验证并在验证后获得正确的类型,但确实是特定的类型。 For example, suppose I'm getting data with type { keyA?: { property?: string | null | undefined }, keyB?: string | null } | null例如,假设我正在data类型为{ keyA?: { property?: string | null | undefined }, keyB?: string | null } | null { keyA?: { property?: string | null | undefined }, keyB?: string | null } | null { keyA?: { property?: string | null | undefined }, keyB?: string | null } | null and I'd like to be sure property is a non empty string, while keyB could be null (just to illustrate I don't want to validate every field, it's not a common NonNull~ case). { keyA?: { property?: string | null | undefined }, keyB?: string | null } | null并且我想确定property是一个非空字符串,而keyB可以是 null(只是为了说明我不想验证每个字段,这不是常见的 NonNull~ 情况)。 Is there a way to do it in another function scope (that receives this data) without manually creating a type?有没有办法在另一个函数范围(接收此数据)中执行此操作而无需手动创建类型?

I tried creating a function:我尝试创建一个函数:

function assertData(data: DType): asserts data {
  if(!data) throw new Error('no data')
  if(!data.keyA) throw new Error('keyA undefined');
  if(!data.keyA.property) throw new Error('property undefined');

After calling the function, I can use data (not null value), but the keyA can be undefined .调用函数后,我可以使用数据(不是空值),但keyA可以是undefined I expected to able to use data.keyA.property without problems and already typed as string.我希望能够毫无问题地使用data.keyA.property并且已经输入为字符串。

You could modify the function to have the following signature:您可以修改该函数以具有以下签名:

function assertData(data: DType): asserts data is DType & { 
  keyA: { property: string } 
} {
  if(!data) throw new Error('no data')
  if(!data.keyA) throw new Error('keyA undefined');
  if(!data.keyA.property) throw new Error('property undefined')
}

We assert that data is DType but also that keyA and keyA.property are not undefined.我们断言dataDType ,而且keyAkeyA.property不是未定义的。

declare const a: DType
assertData(a)
a.keyA.property.charAt(0)

Playground 操场

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

相关问题 将类型断言与解构赋值一起使用的最佳方法是什么? - What's the best way to use a type assertion with destructuring assignment? 有没有办法在创建另一个 object 类型时使用 object 类型的特定值 - Is there a way to use specific values of an object type when creating another object type 如何使用 Typescript 3.7 断言函数声明“不为空”类型断言 - How to declare "is not null" type assertion using Typescript 3.7 Assertion Functions 如何在 TypeScript 中编写类型安全的断言函数? - How to write type safe assertion functions in TypeScript? 创建一个类型为 object 的函数 - Creating a type that is an object of functions 在TypeScript中键入Assertion而不指定新变量 - Type Assertion in TypeScript without assigning a new variable 禁止使用“&lt;&gt;”进行类型断言,请改用“as”? - Type assertion using the '<>' is forbidden,use 'as' instead? 有没有办法使用泛型来键入一个复杂的对象数组来准确比较 React 组件及其 props 类型? - Is there a way to use generics to type a complex object array that accurately compares a React component and its props type? 如何通过angular2,验证仅在输入框中键入特定字符 - how to type only specific characters into inputbox by angular2, validations 如何在@input() 变量上使用类型断言? - How to use type assertion on @input() variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM