小编nas*_*eer的帖子

样式组件/宏不适用于 Jest

我正在尝试将 Jest ( ts-jest) 集成到我使用styled-components/macros. 但由于某种原因,Jest 抱怨以下错误:

\n
ts-jest[versions] (WARN) Version 4.0.2 of typescript installed has not been tested with ts-jest. If you\'re experiencing issues, consider using a supported version (>=4.3.0 <5.0.0-0). Please do not report issues in ts-jest if you are using unsupported versions.\n FAIL  packages/xx/src/components/button.test.tsx\n  \xe2\x97\x8f Test suite failed to run\n\n    TypeError: macro_1.default.button is not a function\n\n      2 |\n      3 | export const Context = {\n    > 4 |   Root: styled.button``,\n        |                      ^\n      5 | …
Run Code Online (Sandbox Code Playgroud)

typescript jestjs babel-jest styled-components ts-jest

11
推荐指数
1
解决办法
725
查看次数

从具有正确类型推断的键数组创建一个新的子集对象

给定一个对象和一个键的字符串数组(这些键将存在于对象中),使用正确的 Typescript 类型构造一个包含这些键及其对应值的新对象。那是:

/**
 * This function will construct a new object
 * which is the subset values associated to 
 * the keys array
**/
function extractFromObj<T>(obj: T, keys: (keyof T)[])

// in here, suppose `post` is a huge object,
// but the intellicence should only show keys
// from the keys array
const { id, properties, created_time } = extractFromObj(post, ['id', 'properties', 'created_time'])
Run Code Online (Sandbox Code Playgroud)

我的解决方案(到目前为止是错误的)

function extractFromObj<T>(obj: T, keys: (keyof T)[]): Record<keyof typeof keys, any> {
  return keys.reduce((newObj, …
Run Code Online (Sandbox Code Playgroud)

typescript

4
推荐指数
1
解决办法
2779
查看次数