question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

extending @types/jasmine interface / namespace doesn't recognise method

See original GitHub issue

I’m using ts-node via jasmine-ts and have a custom matcher which extends the d.ts in @types\jasmine unfortunately ts-node doesn’t seem to pick up the new method.

I’m adding my method to the interface like so:

   declare namespace jasmine {
    interface Matchers<T> {
        toEqualInstruction(expected: IInstruction): boolean;
    }
   }

but ts-node reports:

c:\github\gareththegeek\corewar>npm test

> corewar@1.0.0 test c:\github\gareththegeek\corewar
> nyc jasmine-ts


c:\github\gareththegeek\corewar\node_modules\ts-node\src\index.ts:307
        throw new TSError(formatDiagnostics(diagnosticList, cwd, ts, lineOffset))
              ^
TSError: ⨯ Unable to compile TypeScript
simulator\tests\ExecutiveTests.ts (227,38): Property 'toEqualInstruction' does not exist on type 'Matchers<IInstruction>'. (2339)
simulator\tests\ExecutiveTests.ts (230,38): Property 'toEqualInstruction' does not exist on type 'Matchers<IInstruction>'. (2339)

I’ve posted this on SO here: https://stackoverflow.com/questions/47313578/cannot-extend-a-types-jasmine-interface in case this is not the right place for the issue.

All the info I’ve found says to do what I’ve done but I’m unable to work out quite why the interface isn’t being combined.

I see this issue in vscode intellisense as well so it may be more a .ts issue. Happy to help investigate to get to the bottom of things.

versions below if it’s of any help:

>ts-node --version
ts-node v3.3.0
node v8.9.1
typescript v2.6.1

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
blakeembreycommented, Nov 19, 2017

It works fine for me. There’s plenty of instructions around and unfortunately ts-node isn’t the best place to find them.

  1. It needs to be in a separate .d.ts file because if it’s in the same .ts file all you’re doing is declaring a local namespace (it’s a confusing part of TypeScript, but once you add import or export to the top level of a .ts file it’s a module now, no longer interacting with the global scope)
  2. You need to actually include the extra .d.ts file in compilation. The normal way to do that is by including it via tsconfig.json. You can, however, also use a reference.

Here’s the test that worked locally for me:

npm i @types/jasmine
declare namespace jasmine {
    export interface Matchers<T> {
        toEqualInstruction(expected: any): boolean;
    }
}
/// <reference path="interfaces.d.ts" />

expect(10).toEqualInstruction(10)
tsc test.ts
1reaction
blakeembreycommented, Nov 19, 2017

Thanks, I was just responding that you missed that point 😄

One thing you try try if you need the import is wrapping it all in declare global {}. Haven’t tested it yet, but it might work for your use-case.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Create custom jasmine matcher using Typescript
That seems to indicate that the declare namespace jasmine statement is creating a new jasmine namespace rather than extending the existing one. So...
Read more >
Extending various TypeScript type declarations
Check what is the name of the module / interface / namespace I want to extend. By hovering on process.env I can see...
Read more >
Backbone.js
Backbone.js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable ...
Read more >
Google TypeScript Style Guide
Do not mark interfaces specially ( IMyInterface or MyFooInterface ) unless it's idiomatic in its environment. When introducing an interface for a class,...
Read more >
Configuring Jest
You can retrieve Jest's defaults from jest-config to extend them if ... full list of methods and argument types see Reporter interface in ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found