Reputation: 4984
In the TypeScript handbook is this example for declaration merging:
// observable.js
export class Observable<T> {
// ... implementation left as an exercise for the reader ...
}
// map.ts
import { Observable } from "./observable";
declare module "./observable" {
interface Observable<T> {
map<U>(f: (x: T) => U): Observable<U>;
}
}
Observable.prototype.map = function (f) {
// ... another exercise for the reader
}
// consumer.ts
import { Observable } from "./observable";
import "./map";
let o: Observable<number>;
o.map(x => x.toFixed());
Since Webpack compiles my TypeScript code, I can use the npm module xstream, which ships with this type definitions:
import {Stream, MemoryStream, Listener, Producer, Operator} from './core';
export {Stream, MemoryStream, Listener, Producer, Operator};
export default Stream;
I can use it like this (map
is a valid method):
// consumer.ts
import { Stream } from "xstream";
let s: Stream<number>;
s.map(x => x);
I'd like to extend the Stream
interface and tried something similar to the official example. So in my case it is "xstream"
instead of "./observable"
and my extensions are in the file extensions.ts instead of map.ts:
// extensions.ts
import { Stream } from "xstream";
declare module "xstream" {
interface Stream<T> {
foo(): Stream<T>;
}
}
Stream.prototype.foo = function foo() {
console.log('foo');
return this;
};
// consumer.ts
import { Stream } from "xstream";
import "./extensions";
let s: Stream<number>;
s.foo().map(x => x);
Sadly, I get multiple errors:
ERROR in [default] <path to my project>/node_modules/xstream/index.d.ts:2:9
Export declaration conflicts with exported declaration of 'Stream'
ERROR in [default] <path to my project>/src/extendingExistingModule/extensions.ts:8:0
Cannot find name 'Stream'.
ERROR in [default] <path to my project>/src/extendingExistingModule/index.ts:5:8
Property 'map' does not exist on type 'Stream<number>'.
How can I fix them?
used versions
[email protected]
[email protected]
Upvotes: 5
Views: 584