TypeScript Version: nightly (2.0.0-dev.201xxxxx)
Code
npm i @types/estree;
// myESTreeExtension.ts
declare module 'estree' {
interface MyAddon {
}
}
// Some other file.ts
import * as estree from 'estree';
let a: estree.Node; // ERROR: [ts] Module ''estree'' has no exported member 'Node'.
Expected behavior:
I would expect i can extend an external module as described here: #280
Actual behavior:
The ambient module in myESTreeExtension.ts
_overrides_ the @types/estree
types
Remark: I'm a big fan of the new work flow with @types. Great job!
make sure the file that contains the module augmentation is itself a module (i.e. includes at least one top-level import
or export
).
so this should work:
import * as estree from 'estree';
declare module 'estree' {
interface MyAddon {
}
}
Thanks @mhegazy, this works. When using the extensions you need to do this:
import * as estree from 'estree';
import 'myESTreeExtension';
let a: estree.Node; // Works
let b: estree.MyAddon; // Works as well
Sorry to ask here since the bug is closed but i have a similar problem with KoaJS definitions here.
The Koa definition file looks like this:
declare namespace Koa { }
export = Koa;
declare namespace Koa {
export interface Context extends Request, Response {
}
// ... more things
}
declare class Koa extends EventEmitter {
// ... things
}
And i want to be able to do something like (as suggested here and here):
import * as Koa from 'koa';
const server: Koa = new Koa();
server.name = 'UrbanScope';
server.context.db = db; // server.context is Koa.Context
But i keep getting TS2339 errors from tsc
like:
src/server.ts(60,10): error TS2339: Property 'name' does not exist on type 'Koa'.
src/server.ts(63,18): error TS2339: Property 'db' does not exist on type 'Context'.
Following your advice i created a augment.ts
file with:
import * as Koa from 'koa';
import { Db } from 'mongodb';
declare module 'koa' {
namespace Koa {
interface Context {
db: Db,
}
}
class Koa {
name?: string;
}
}
But the errors are still there 馃槰 , i tried with a number of export/namespace/class/declare combinations with no luck. Any idea on how to solve it?
given the way Koa is authored extending the class is not possible. as for the interface context, you should be able to do that as:
import * as Koa from 'koa';
import { Db } from 'mongodb';
declare module 'koa' {
interface Context {
db: Db,
}
}
It works!
Thanks for you support!
And what should I do if package name starts with @
?
I'm trying to extend a class, but it seems to override the whole class with my new definition. F.e.:
import * as THREE from 'three' // includes a definition for OrbitControls
declare module 'three' {
export class OrbitControls {
setCamera( cam: THREE.Camera ): void
}
}
Now my code has no errors when using the setCamera
method, but there is now a type error on the use of all the other stuff in the OrbitControls
class, so it looks like the @types/three
version was replaced with my version.
How do we extend the class defintion of a module?
@trusktr r i believe you should do this instead
import * as THREE from 'three' // includes a definition for OrbitControls
declare module 'three' {
export interface OrbitControls {
setCamera( cam: THREE.Camera ): void
}
}
Most helpful comment
make sure the file that contains the module augmentation is itself a module (i.e. includes at least one top-level
import
orexport
).so this should work: