angular 2 typescript An implementation cannot be declared in ambient contexts
Asked Answered
G

7

32

I am new to typescript and I am trying to create a function for an angular 2 directive. Can anyone explain, in language for n00bs, what the error is trying to tell me when I am compiling with Gulp?

An implementation cannot be declared in ambient contexts

The message applies to offset() and toggler().

import { Directive, ElementRef, Input } from '@angular/core';

@Directive({
  selector: 'offCanvas',
  inputs: ['target', 'toggle', 'placement', 'autohide', 'recalc', 'disableScrolling', 'modal', 'canvas', 'exclude'],
  host: {
    '(click)': 'Click()'
  }
})

export class OffCanvas {  
  @Input('target') target: string;
  @Input('canvas') canvas: string;
  @Input('state') state: string;
  @Input('exclude') exclude: string;
  @Input('placement') placement: string;
  @Input('toggle') toggle: boolean;
  @Input('autohide') autohide: boolean;
  @Input('recalc') recalc: boolean;
  @Input('disableScrolling') disableScrolling: boolean;
  @Input('modal') modal: boolean;

  public offset() {
    switch (this.placement) {
      case 'left':
      case 'right':  return (<HTMLElement>document.querySelector(this.target)).offsetWidth
      case 'top':
      case 'bottom': return (<HTMLElement>document.querySelector(this.target)).offsetHeight
    }
  }

  public toggler() {
    if (this.state === 'slide-in' || this.state === 'slide-out') return
    this[this.state === 'slid' ? 'hide' : 'show']()
  }

  Click() {
    this.toggler()
  }
}
Go answered 25/5, 2016 at 22:10 Comment(0)
S
54

An implementation cannot be declared in ambient contexts

You most probably have your file named as foo.d.ts instead of foo.ts. That marks it as a declaration file (more on that https://basarat.gitbooks.io/typescript/content/docs/types/ambient/d.ts.html) and you cannot put logic in these as you are declaring what logic exists elsewhere.

Smote answered 26/5, 2016 at 1:39 Comment(1)
Had this exact issue, was accidentally editing a compiled file...Lapointe
M
23

I ran across this rather unusual compile-time Typescript error error TS1183: An implementation cannot be declared in ambient contexts.

It started after I had copy/pasted some sourcecode that had export declare class. I noticed when I changed it to export class this error went away.

Change This:

export declare class Foo {
}

To This:

export class Foo {
}
Morgen answered 19/11, 2019 at 21:33 Comment(0)
B
9

I fixed it by doing: npm install rxjs

Bailly answered 20/6, 2018 at 16:17 Comment(1)
Thanks. After doing this I have a clean compile -- no more rxjs "ambient contexts" and "duplicate function implementation" errors.Jacobba
F
3

I think this happened to me, because I accidentally edited one of the files in node_modules. This fixed it for me:

rm -r node_modules
npm install
Fiasco answered 29/9, 2018 at 1:23 Comment(0)
T
1

I had the same error and fixed it by typing:

npm install --save typescript@latest

package.json now has the latest ts version and ts compiler is 2.2.2.

Tropophilous answered 26/4, 2017 at 16:27 Comment(1)
That gave me a new error telling me I needed to back down to a lower version of typescript as my compiler was not compatible with the latest version. After back it down back to this error. So feels like I am in a loop.Jacket
P
1

error TS1183: An implementation cannot be declared in ambient contexts. Solution :Removed node module and reinstall it.

Petty answered 20/3, 2018 at 10:48 Comment(2)
Have same issue. Brand New Node Install pulled down v8.11.2 just yesterday npm v 5.6.0 and the issue is still there.Jacket
this is big problem me too have the same issue !Gyrocompass
L
0

I was facing this issue for the constructor implementation. So, basically, we need to avoid the implementation {...} while defining the class.

Instead of this:

declare module MyModlue {
  class MyClass {
    constructor(type: string) {
      // implementation
    };
  }
}

I did this:

declare module MyModlue {
  class MyClass {
    constructor(type: string); // notice that it doesn't have implementation
  }
}
Lakisha answered 12/3, 2022 at 8:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.