2

I have a Angular component, And I add a type to the Input property with namespace as follow code. @Input() size: AMap.Size

AMap is a namespace and the class Size with in. The IDE and typescript compiler work well.But, It's not work in browser。 And throw an error of Uncaught ReferenceError: AMap is not defined I found the compiled code

enter image description here

Why the type will compile into the code?

The AMap types as follow

declare namespace AMap {

    export class Size {}

}

thanks

6
  • that AMap namespace, is yours? I mean, belongs to your project, or comes from an external library Commented May 3, 2018 at 6:47
  • @OscarPaz Yeah, It's mine. and in my project. Commented May 3, 2018 at 6:50
  • Can you show us the code of the file in which it is defined? Commented May 3, 2018 at 6:52
  • The file is in src/types/amap/index.d.ts, And the config of tsconfig.json is ``` "typeRoots": [ "node_modules/@types", "src/types" ] ``` Commented May 3, 2018 at 6:55
  • Yes, but, can you post the contents? Commented May 3, 2018 at 6:57

2 Answers 2

1

What worked for me was using a type alias at the top of the file and then using that.

type ASize = AMap.Size

...

@Input() size: ASize

I know that this is only a workaround, but maybe it is of use to someone.

Sign up to request clarification or add additional context in comments.

1 Comment

I don't like this solution but it did the trick for me as well.
0

I had a similar problem. I think that was related to Typescript, webpack bundling issue. Can you try to use:

export module AMap {
   export class Size {...}
}

instead of export namespace. And import it and use in component like:

import { AMap } from './../..';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.