[SOLVED] Problem to extend native Typescript types


#1

Hi,

first of all, this is my app info:

Cordova CLI: 6.3.1
Ionic Framework Version: 2.0.0-rc.1
Ionic CLI Version: 2.1.0
Ionic App Lib Version: 2.1.0-beta.1
OS:
Node Version: v6.7.0

Ok… I add a file named extensions.ts inside the src directory of my project. The file contains some extensions of Typescript primtive types. For example:

/**
 * An extended version of the native Navigator class
 */
interface Navigator {
    /**
     * Returns the current language of the user
     */
    currentLang() : string;
}

Navigator.prototype.currentLang = function() : string {
    var userLang = navigator.language.split('-')[0];
    userLang = /(en|it)/gi.test(userLang) ? userLang : 'en';
    return userLang;
};

When I run ionic serve the Google chrome console gives me the following error:

TypeError: navigator.currentLang is not a function

I tried to insert this file on the top of the "include" section of tsconfig.json:

"include": [
    "src/extensions.ts",
    "src/**/*"
  ],

but the problem persists.

What Can I do to solve this issue?

Thanks in advance.


#2

IIRC you have to wrap your interface extension inside declare global{}.


#3

Thanks.

Now, the ts compiler gives me the following error:

Augmentations for the global scope can only be directly nested in external modules or ambient module declarations.

??

EDIT:

Simply by adding as the first line:

export {};

the compiler error is not shown anymore, but the problem with not found function persists.:confused:


#4

When I run ionic build the following error is shown:

[00:44:24] TypeError: Cannot read property ‘text’ of undefined
[00:44:24] ngc failed
[00:44:24] ionic-app-script task: “build”
[00:44:24] Error: Error

npm ERR! Windows_NT 10.0.14393
npm ERR! argv “C:\nodejs\node.exe” “C:\nodejs\node_modules\npm\bin\npm-cli.js” “run” "build"
npm ERR! node v6.9.2
npm ERR! npm v3.10.9
npm ERR! code ELIFECYCLE
npm ERR! ionic-hello-world@ build: ionic-app-scripts build
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the ionic-hello-world@ build script ‘ionic-app-scripts build’.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the ionic-hello-world package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! ionic-app-scripts build
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs ionic-hello-world
npm ERR! Or if that isn’t available, you can get their info via:
npm ERR! npm owner ls ionic-hello-world
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR! E:<project dir>\npm-debug.log

The content of the npm-debug.log file is the following:

0 info it worked if it ends with ok
1 verbose cli [ ‘C:\nodejs\node.exe’,
1 verbose cli ‘C:\nodejs\node_modules\npm\bin\npm-cli.js’,
1 verbose cli ‘run’,
1 verbose cli ‘build’ ]
2 info using npm@3.10.9
3 info using node@v6.9.2
4 verbose run-script [ ‘prebuild’, ‘build’, ‘postbuild’ ]
5 info lifecycle ionic-hello-world@~prebuild: ionic-hello-world@
6 silly lifecycle ionic-hello-world@~prebuild: no script for prebuild, continuing
7 info lifecycle ionic-hello-world@~build: ionic-hello-world@
8 verbose lifecycle ionic-hello-world@~build: unsafe-perm in lifecycle true
9 verbose lifecycle ionic-hello-world@~build: PATH: C:\nodejs\node_modules\npm\bin\node-gyp-bin;E:<project dir>\node_modules.bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\php;C:\ProgramData\ComposerSetup\bin;C:\android-sdk\platform-tools;C:\android-sdk\tools;C:\Users\Utente\AppData\Roaming\npm
10 verbose lifecycle ionic-hello-world@~build: CWD: E:<project dir>
11 silly lifecycle ionic-hello-world@~build: Args: [ ‘/d /s /c’, ‘ionic-app-scripts build’ ]
12 silly lifecycle ionic-hello-world@~build: Returned: code: 1 signal: null
13 info lifecycle ionic-hello-world@~build: Failed to exec build script
14 verbose stack Error: ionic-hello-world@ build: ionic-app-scripts build
14 verbose stack Exit status 1
14 verbose stack at EventEmitter. (C:\nodejs\node_modules\npm\lib\utils\lifecycle.js:255:16)
14 verbose stack at emitTwo (events.js:106:13)
14 verbose stack at EventEmitter.emit (events.js:191:7)
14 verbose stack at ChildProcess. (C:\nodejs\node_modules\npm\lib\utils\spawn.js:40:14)
14 verbose stack at emitTwo (events.js:106:13)
14 verbose stack at ChildProcess.emit (events.js:191:7)
14 verbose stack at maybeClose (internal/child_process.js:877:16)
14 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5)
15 verbose pkgid ionic-hello-world@
16 verbose cwd E:<project dir>
17 error Windows_NT 10.0.14393
18 error argv “C:\nodejs\node.exe” “C:\nodejs\node_modules\npm\bin\npm-cli.js” “run” "build"
19 error node v6.9.2
20 error npm v3.10.9
21 error code ELIFECYCLE
22 error ionic-hello-world@ build: ionic-app-scripts build
22 error Exit status 1
23 error Failed at the ionic-hello-world@ build script ‘ionic-app-scripts build’.
23 error Make sure you have the latest version of node.js and npm installed.
23 error If you do, this is most likely a problem with the ionic-hello-world package,
23 error not with npm itself.
23 error Tell the author that this fails on your system:
23 error ionic-app-scripts build
23 error You can get information on how to open an issue for this project with:
23 error npm bugs ionic-hello-world
23 error Or if that isn’t available, you can get their info via:
23 error npm owner ls ionic-hello-world
23 error There is likely additional logging output above.
24 verbose exit [ 1, true ]


#5

Updating Ionic the problem is not shown anymore.