[英]How to describe the interface of a simple Just functor in typescript?
I'm trying to write a simple interface in typescript for the first time and having doubts about pretty much everything. 我试图第一次用打字稿编写一个简单的界面,并对几乎所有内容都有疑问。
The question is simple. 问题很简单。 How to describe this simple jest matcher extension?
如何描述这个简单的玩笑匹配器扩展?
/**
* @param {*} v Any value
*/
function just (v) {
return {
fmap: f => just(f(v))
}
}
expect.extend({
/** Compare the two values inside two functors with Object.is
* @method
* @augments jest.Matchers
* @param {*} actual The functor you want to test.
* @param {*} expected The functor you expect.
*/
functorToBe(actual, expected) {
const actualValue = getFunctorValue(actual)
const expectedValue = getFunctorValue(expected)
const pass = Object.is(actualValue, expectedValue)
return {
pass,
message () {
return `expected ${actualValue} of ${actual} to ${pass ? '' : 'not'} be ${expectedValue} of ${expected}`
}
}
}
})
test('equational reasoning (identity)', () => {
expect(just(1)).functorToBe(just(1))
})
I have tried this but not sure about the generic types at all: 我已经尝试过了,但是根本不确定泛型类型:
import { Matchers } from 'jest'
interface Functor<T> {
(value?: any): {
fmap: (f: value) => Functor<T>
}
}
interface Matchers<R> {
functorToBe(actual: Functor<T>, expected: Functor<T>): R
}
Ref: JSDoc document object method extending other class 参考: 扩展其他类的JSDoc文档对象方法
The gist of the jest type definition for Matchers<R>
: Matchers<R>
的笑话类型定义的要点:
/**
* The `expect` function is used every time you want to test a value.
* You will rarely call `expect` by itself.
*/
interface Expect {
/**
* The `expect` function is used every time you want to test a value.
* You will rarely call `expect` by itself.
*
* @param actual The value to apply matchers against.
*/
<T = any>(actual: T): Matchers<T>;
/**
* You can use `expect.extend` to add your own matchers to Jest.
*/
extend(obj: ExpectExtendMap): void;
// etc.
}
Well, this is confusing. 好吧,这令人困惑。 The only index.d.ts in the jest repo is https://github.com/facebook/jest/blob/master/packages/jest-editor-support/index.d.ts but that is not the one I get in vscode, which is https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/jest/index.d.ts#L471 .
笑话回购中唯一的index.d.ts是https://github.com/facebook/jest/blob/master/packages/jest-editor-support/index.d.ts,但这不是我能得到的vscode,它是https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/jest/index.d.ts#L471 。
Your merge of Matchers
is almost right, but from looking at the jest types on Definitely Typed [1], I can see that Matchers
is nested inside a jest
namespace. 您对
Matchers
合并几乎是正确的,但是通过查看Definitely Typed [1]上的jest类型,我可以看到Matchers
嵌套在jest
名称空间内。 The part of the Typescript handbook that covers this is namespace merging . Typescript手册涵盖这一部分的部分是名称空间合并 。
namespace jest {
export interface Matchers<R> {
functorToBe<T>(actual: Functor<T>, expected: Functor<T>): R;
}
}
When I tested this, I needed a tsconfig.json to get my JS files to see the declaration: 当我对此进行测试时,我需要一个tsconfig.json来获取我的JS文件以查看声明:
{
"compilerOptions": {
"allowJs": true,
"checkJs": true,
},
"include": [
"test.js",
"extensions.ts",
]
}
A jsconfig.json is almost the same thing, but doesn't require you to add "allowJs": true
explicitly. jsconfig.json几乎是同一件事,但是不需要您添加
"allowJs": true
显式为"allowJs": true
。 But note that adding a tsconfig or jsconfig will turn off automatic downloads of types from Definitely Typed, and you'll have to manually npm install @types/jest
(plus any other types for libraries you use). 但是请注意,添加tsconfig或jsconfig会关闭从“确定类型”中自动下载类型,并且您必须手动
npm install @types/jest
(以及您使用的库的其他任何类型)。 If you don't want to add a tsconfig, I was able to manually add a reference in the JS file as the first line: 如果您不想添加tsconfig,则可以在JS文件中手动添加引用作为第一行:
/// <reference path="./extensions.ts" />
It should be something like: 应该是这样的:
functorToBe(expected: R): R;
Here is where things get confusing to me. 这是让我感到困惑的地方。 Let me know if you have questions about this part of the problem and I will try to help.
如果您对问题的这一部分有疑问,请告诉我,我将尽力提供帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.