<#1054 Question: How to use TypeScript interfaces ...
# pact-js-development
g
#1054 Question: How to use TypeScript interfaces with matchers Issue created by agross Hello, this is a question rather than a bug report or feature request. I'm quite new to Pact and I would like to reuse the TypeScript interfaces my frontend app uses for the consumer-side of things. The documentation about matching states that in order to use TypeScript interfaces inside e.g.
willRespondWith/body
you need to wrap them in
InterfaceToTemplate
or use
type
instead of interface. I've tried both but the results where not quite satisfactory. As far as I understand it would be beneficial to use existing `type`s and `interface`s to prevent typos and to define the correct expected value types for the mock server. On the other hand,
Matcher.<something>
seems incompatible with the types expected by interface members. The example from the docs only uses the
like
matcher around the interface, but not for its members. My question is how I would be able to achieve something like this with Pact:
Copy code
interface Foo {
  a: string;
}

const f: InterfaceToTemplate<Foo> = { a: like("working example") };

// The above causes:
// Type 'Matcher<"working example">' is not assignable to type 'AnyTemplate'.
//    Type 'Matcher<"working example">' is not assignable to type 'TemplateMap'.
//       Index signature for type 'string' is missing in type 'Matcher<"working example">'.

provider.addInteraction({
  uponReceiving: "a post with foo",
  withRequest: {
    method: "POST",
    path: "/",
    body: like(f)
  },
  ...
})
The only thing that comes to mind is to slap
as unknown as string
after the matcher. Software versionsOS: macOS Ventura • Consumer Pact library:
Pact JS 10.4.1
Node Version:
v16.19.0
pact-foundation/pact-js