Office-js: typescript definitions file (latest and beta) produces typescript errors on compile

Created on 27 Jul 2018  路  11Comments  路  Source: OfficeDev/office-js

Expected Behavior

My excel angular add-in project should build.

Current Behavior

Instead, on building or serving, I get typescript errors (shown below in useful logs.

Steps to Reproduce, or Live Example

  • Reproduction steps:

    1. npm i -g @angular/cli

    2. ng new example && cd example

    3. npm i @microsoft/office-js

    4. add a typings.d.ts file to src/ with the line /// <reference types="@microsoft/office-js/dist/office" /> in it

    5. npm run build and you'll see the typescript errors

In step 3, if you do npm i @microsoft/office-js@beta which is what I really want, the story is the same.

Context

Previously, I was using @types/office-js and the reference would just be /// <reference types="office-js" /> and my build process would work just fine. But I'm working on a home-brew excel add-in and want to work with the latest features in beta release. Specifically, access to a chart's secondary axis is read-only in the latest release but can be set in beta. So I need a way to work with the beta release and with its typescript definitions file. If I am referencing the typescript definition file incorrectly, I need help with that. If its a bug in the typescript file, it needs to be fixed.

Your Environment

  • Platform [PC desktop, Mac, iOS, Office Online]: PC Desktop
  • Host [Excel, Word, PowerPoint, etc.]: Excel
  • Office version number: ?
  • Operating System: Windows 10
  • Browser (if using Office Online): NA

Useful logs

  • [x] Console errors
> npm run build

> [email protected] build C:\Users\Troy W\Documents\Code\test\example
> ng build


Date: 2018-07-27T16:39:23.903Z
Hash: 300ee9abcacf65278ea4
Time: 3055ms
chunk {main} main.js, main.js.map (main) 634 bytes [initial] [rendered]
chunk {polyfills} polyfills.js, polyfills.js.map (polyfills) 689 bytes [initial] [rendered]
chunk {runtime} runtime.js, runtime.js.map (runtime) 5.22 kB [entry] [rendered]
chunk {styles} styles.js, styles.js.map (styles) 15.6 kB [initial] [rendered]

ERROR in node_modules/@microsoft/office-js/dist/office.d.ts(2593,9): error TS1038: A 'declare' modifier cannot be used in an already ambient context.
node_modules/@microsoft/office-js/dist/office.d.ts(2601,9): error TS1038: A 'declare' modifier cannot be used in an already ambient context.
node_modules/@microsoft/office-js/dist/office.d.ts(2650,9): error TS1038: A 'declare' modifier cannot be used in an already ambient context.
node_modules/@microsoft/office-js/dist/office.d.ts(2791,11): error TS2300: Duplicate identifier 'RequestContext'.
node_modules/@microsoft/office-js/dist/office.d.ts(2798,84): error TS2694: Namespace 'OfficeExtension' has no exported member 'IPromise'.
node_modules/@microsoft/office-js/dist/office.d.ts(2798,114): error TS2694: Namespace 'OfficeExtension' has no exported member 'IPromise'.
node_modules/@microsoft/office-js/dist/office.d.ts(2804,120): error TS2694: Namespace 'OfficeExtension' has no exported member 'IPromise'.
node_modules/@microsoft/office-js/dist/office.d.ts(2804,150): error TS2694: Namespace 'OfficeExtension' has no exported member 'IPromise'.
node_modules/@microsoft/office-js/dist/office.d.ts(2810,122): error TS2694: Namespace 'OfficeExtension' has no exported member 'IPromise'.
node_modules/@microsoft/office-js/dist/office.d.ts(2810,152): error TS2694: Namespace 'OfficeExtension' has no exported member 'IPromise'.
node_modules/@microsoft/office-js/dist/office.d.ts(2816,125): error TS2694: Namespace 'OfficeExtension' has no exported member 'IPromise'.
node_modules/@microsoft/office-js/dist/office.d.ts(2816,155): error TS2694: Namespace 'OfficeExtension' has no exported member 'IPromise'.
node_modules/@microsoft/office-js/dist/office.d.ts(2826,66): error TS2694: Namespace 'OfficeExtension' has no exported member 'IPromise'.
node_modules/@microsoft/office-js/dist/office.d.ts(2827,59): error TS2694: Namespace 'OfficeExtension' has no exported member 'IPromise'.
node_modules/@microsoft/office-js/dist/office.d.ts(3427,11): error TS2300: Duplicate identifier 'RequestContext'.
node_modules/@microsoft/office-js/dist/office.d.ts(36393,78): error TS2694: Namespace 'OfficeExtension' has no exported member 'IPromise'.
node_modules/@microsoft/office-js/dist/office.d.ts(36393,108): error TS2694: Namespace 'OfficeExtension' has no exported member 'IPromise'.
node_modules/@microsoft/office-js/dist/office.d.ts(36399,116): error TS2694: Namespace 'OfficeExtension' has no exported member 'IPromise'.
node_modules/@microsoft/office-js/dist/office.d.ts(36399,146): error TS2694: Namespace 'OfficeExtension' has no exported member 'IPromise'.
node_modules/@microsoft/office-js/dist/office.d.ts(36405,119): error TS2694: Namespace 'OfficeExtension' has no exported member 'IPromise'.
node_modules/@microsoft/office-js/dist/office.d.ts(36405,149): error TS2694: Namespace 'OfficeExtension' has no exported member 'IPromise'.
node_modules/@microsoft/office-js/dist/office.d.ts(42236,81): error TS2694: Namespace 'OfficeExtension' has no exported member 'IPromise'.
node_modules/@microsoft/office-js/dist/office.d.ts(42236,111): error TS2694: Namespace 'OfficeExtension' has no exported member 'IPromise'.
node_modules/@microsoft/office-js/dist/office.d.ts(42242,119): error TS2694: Namespace 'OfficeExtension' has no exported member 'IPromise'.
node_modules/@microsoft/office-js/dist/office.d.ts(42242,149): error TS2694: Namespace 'OfficeExtension' has no exported member 'IPromise'.
node_modules/@microsoft/office-js/dist/office.d.ts(42248,122): error TS2694: Namespace 'OfficeExtension' has no exported member 'IPromise'.
node_modules/@microsoft/office-js/dist/office.d.ts(42248,152): error TS2694: Namespace 'OfficeExtension' has no exported member 'IPromise'.
node_modules/@microsoft/office-js/dist/office.d.ts(44477,52): error TS2694: Namespace 'OfficeExtension' has no exported member 'EmbeddedSession'.
node_modules/@microsoft/office-js/dist/office.d.ts(44484,79): error TS2694: Namespace 'OfficeExtension' has no exported member 'IPromise'.
node_modules/@microsoft/office-js/dist/office.d.ts(44484,109): error TS2694: Namespace 'OfficeExtension' has no exported member 'IPromise'.
node_modules/@microsoft/office-js/dist/office.d.ts(44490,76): error TS2694: Namespace 'OfficeExtension' has no exported member 'EmbeddedSession'.
node_modules/@microsoft/office-js/dist/office.d.ts(44490,151): error TS2694: Namespace 'OfficeExtension' has no exported member 'IPromise'.
node_modules/@microsoft/office-js/dist/office.d.ts(44490,181): error TS2694: Namespace 'OfficeExtension' has no exported member 'IPromise'.
node_modules/@microsoft/office-js/dist/office.d.ts(44496,132): error TS2694: Namespace 'OfficeExtension' has no exported member 'IPromise'.
node_modules/@microsoft/office-js/dist/office.d.ts(44496,162): error TS2694: Namespace 'OfficeExtension' has no exported member 'IPromise'.
node_modules/@microsoft/office-js/dist/office.d.ts(44502,120): error TS2694: Namespace 'OfficeExtension' has no exported member 'IPromise'.
node_modules/@microsoft/office-js/dist/office.d.ts(44502,150): error TS2694: Namespace 'OfficeExtension' has no exported member 'IPromise'.

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] build: `ng build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Troy W\AppData\Roaming\npm-cache\_logs\2018-07-27T16_39_23_941Z-debug.log
has workaround product bug

Most helpful comment

Hi @troywweber7 ,

It's a known issue -- the d.ts files between what we have on DefinitelyTyped and what we have on the CDN/NPM package have unfortunately diverged, and we need to do some work to bring them together again. For the time being, DefinitelyTyped (or @types/office-js, which is auto-generated by the TypeScript bot off of it) is the source of truth.

This should be resolved in the coming weeks. In the meantime, if you need the Beta APIs, one workaround you could do is to:

  1. Create your own new file
  2. Paste the contents from https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/master/types/office-js/index.d.ts into it.
  3. Delete everything between the block titled "Begin Excel APIs" and the block "End Excel APIs".
  4. Now open the beta APIs from https://appsforoffice.microsoft.com/lib/beta/hosted/Office.d.ts (latest beta CDN) and copy the block between "Begin Excel APIs" and "End Excel APIs" to the clipboard, pasting it back in place of the block you deleted in step 3.

At the end, you'll end up with a file identical to what I have attached (saving it as ".txt" since github didn't let me do "d.ts"). This should get you unblocked for the present. Sorry for the inconvenience.
office-js-beta.txt

All 11 comments

Hi @troywweber7 ,

It's a known issue -- the d.ts files between what we have on DefinitelyTyped and what we have on the CDN/NPM package have unfortunately diverged, and we need to do some work to bring them together again. For the time being, DefinitelyTyped (or @types/office-js, which is auto-generated by the TypeScript bot off of it) is the source of truth.

This should be resolved in the coming weeks. In the meantime, if you need the Beta APIs, one workaround you could do is to:

  1. Create your own new file
  2. Paste the contents from https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/master/types/office-js/index.d.ts into it.
  3. Delete everything between the block titled "Begin Excel APIs" and the block "End Excel APIs".
  4. Now open the beta APIs from https://appsforoffice.microsoft.com/lib/beta/hosted/Office.d.ts (latest beta CDN) and copy the block between "Begin Excel APIs" and "End Excel APIs" to the clipboard, pasting it back in place of the block you deleted in step 3.

At the end, you'll end up with a file identical to what I have attached (saving it as ".txt" since github didn't let me do "d.ts"). This should get you unblocked for the present. Sorry for the inconvenience.
office-js-beta.txt

@Zlatkovsky Thanks so much for the workaround. If I have difficulty with implementing the workaround, I'll come back here!

@Zlatkovsky I was not able to reproduce the file myself, but your .txt saved as a .d.ts works great!

Glad I could help :-)

Thanks from me also - am using Excel Beta APIs and wasn't sure if there was something wrong with the file or I'd set up my project incorrectly.

This should be resolved in the coming weeks

It has now been a couple of months, is the fix in the horizon somewhere?

@zlepper, it should be fixed as of a week or two ago.
Could you try the https://www.npmjs.com/package/@microsoft/office-js/v/1.1.9-beta.0 for Beta, or the 1.1.10 version for the latest Release APIs (which now include ExcelApi 1.8)?

Closing for now, feel free to re-open if you are still encountering an issue

@Zlatkovsky So if the issue is fixed then after installing the projects npm i -D @microsoft/office-js how do I include the office.d.ts file into my Typescript project? A triple-slash directive doesn't seem to work still.

@Zlatkovsky NVM, I think I answered my own question. The proper triple-slash directive is: /// <reference types="../node_modules/@microsoft/office-js/dist/office" />

Yes, that will work. The typings are also available in standalone form on npm install --save-dev @types/office-js.

That is, only use the NPM package if you can't use the CDN (because of firewall, or for offline debugging, etc.). For Store add-ins, they need to reference the CDN anyway. In which case you'd be better off only getting the types, from @types/office-js (which mirrors what's on DefinitelyTyped)

Was this page helpful?
0 / 5 - 0 ratings