简体   繁体   English

键入模拟商店的最佳方式是什么?

[英]what's the best way to type a mock store?

Our test code is riddled with code like this in our tests...我们的测试代码在我们的测试中充斥着这样的代码......

const mockState = {
      client: {
        users: {
          results: {
            something: -91893.21,
          },
        },
      },
    };

Obviously mockState is untyped.显然 mockState 是无类型的。 I can't type it as RootState since that interface has heaps of properties that this test doesn't need.我不能将其键入为 RootState,因为该接口具有该测试不需要的大量属性。

I looked at Partial and DeepPartial but they don't really help since any json would implement any DeepPartial since every property would be optional.我查看了 Partial 和 DeepPartial 但它们并没有真正帮助,因为任何 json 都会实现任何 DeepPartial,因为每个属性都是可选的。 Is there a way to type a json object so that all it's properties match a specific type, but not all properties are required.有没有办法输入 json object 以便它的所有属性都匹配特定类型,但并非所有属性都是必需的。 Confusing question.令人困惑的问题。 I want to type mockState to IWhatever so that it won't compile unless IWhatever has a property called client, which has a property called users etc.我想将 mockState 输入到 IWhatever 以便它不会编译,除非 IWhatever 有一个名为 client 的属性,它有一个名为 users 等的属性。

If you do the assignment at the same time as you do the variable definitionobject freshness kicks in and will prevent you from adding any additional properties.如果您在执行变量定义object的同时进行赋值,那么新鲜度就会启动,并且会阻止您添加任何其他属性。

Example例子

type RootState = {
  client: {
    a: {
      something: number
    },
    b: {
      something: number
    }
  },
  server: {
    a: {
      something: number
    },
    b: {
      something: number
    }
  }
}

// Reference DeepPartial
type DeepPartial<T> = {
  [P in keyof T]?: T[P] extends Array<infer U>
    ? Array<DeepPartial<U>>
    : T[P] extends ReadonlyArray<infer U>
      ? ReadonlyArray<DeepPartial<U>>
      : DeepPartial<T[P]>
};


// Okay 
const mockState: DeepPartial<RootState> = {
  client: {
    a: {
      something: 123
    }
  }
}

// Not Okay 
const mockStateBad: DeepPartial<RootState> = {
  client: {
    a: {
      somethings: 123 // ERROR 
    }
  }
}

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

相关问题 将类型断言与解构赋值一起使用的最佳方法是什么? - What's the best way to use a type assertion with destructuring assignment? 将索引 object 添加到 Redux 商店(Redux 工具包)的最佳方法是什么? - What's the best way to add an indexed object to a Redux store (Redux Toolkit)? 从 Typescript 中的联合类型推断回调参数类型的最佳方法是什么? - What's the best way to infer the callback argument type from a union type in Typescript? 以编程方式将类型传递给服务的最佳方法是什么? - what is the best way to programmatically pass a type to a service? 定义一种动态回调的最佳方式是什么? - What is the best way to define a type of dynamic callback? 使用返回与其参数相同类型的可区分联合来声明 TypeScript 函数的最佳方法是什么? - What's the best way to declare a TypeScript function with a discriminated union that returns the same type as its parameter? 在Angular 4中使用权限的最佳方法是什么? - What's the best way to work with permissions in Angular 4? 编写指令的最佳方法是什么? - What's the best way to write directives? 在 URL 中动态存储查询参数的最佳方法是什么? - What is the best way to store query paramaters in the URL dynamically? 使用 Promise.all 存储不同类型数据的最佳方法是什么? - What is the best way to store different types of data with a Promise.all?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM