我正在尝试将 Jest ( ts-jest) 集成到我使用styled-components/macros. 但由于某种原因,Jest 抱怨以下错误:
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 类型构造一个包含这些键及其对应值的新对象。那是:
/**
* 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)