PptxGenJS
PptxGenJS copied to clipboard
[BUG] - Use type on NodeJs context and Typescript langage
Category
- [ ] Enhancement
- [X] Bug
- [ ] Question
- [ ] Documentation gap/issue
Version
- Lib version : 3.4.0
- PowerPoint you are targeting: any
- "ts-node": "^4.1.0",
- "tslint": "5.3.2",
- "@types/node": "^9.6.41",
Observed Behavior
- When we try to use TS type not all types is available (exported) by pptxgenjs library. Error on compilation :
error TS2322: Type '{ charSpacing?: number; fit?: "none" | "shrink" | "resize"; fill?: ShapeFillProps; flipH?: boolea...' is not assignable to type 'TextPropsOptions'.
Types of property 'align' are incompatible.
Type 'string' is not assignable to type 'HAlign'.
OR
error TS2322: Type '{ charSpacing?: number; fit?: "none" | "shrink" | "resize"; fill?: ShapeFillProps; flipH?: boolea...' is not assignable to type 'TextPropsOptions'.
Types of property 'align' are incompatible.
Type 'AlignH | "left" | "center" | "right" | "justify"' is not assignable to type 'HAlign'.
Type 'AlignH' is not assignable to type 'HAlign'.
- So we can't import HAlign type from ppxtgenjs lib.
Steps to Reproduce
- note : this bug is different as object instanciation subject. one issue on this repository say to create instance with :
const PptxFile = require('pptxgenjs');
const pptx = new PptxFile();
- Import PptxGenJS :
import PptxGenJS from 'pptxgenjs';
// create custom function as
static optionH1(options: IPptxGeneratorOptions, params: TextPropsOptions = {}): TextPropsOptions {
return {
...{
w: '100%',
h: '100%',
fontSize: 44,
align: 'center',
color: options.config.colorPrimary,
},
...params,
};
}
- Try to replace 'center' by enum type as = KO
import AlignH = PptxGenJS.AlignH;
[...]
{
...{
w: '100%',
h: '100%',
fontSize: 44,
align: AlignH.center,
},
...params,
}
- Try to replace 'center' by enum type as = KO
{
...{
w: '100%',
h: '100%',
fontSize: 44,
align: HAlign.center,
},
...params,
}
- Compile with tsc :
npm run build
Thx for your job
Are you sure about your setup?
Please check the React Demo for TypeScript examples, including HAlign
Thx for answer.
I'm not sure but React Demo seen to be Browser context with JS langage.
My context is NodeJs and TS langage (NestJs)
- Another tests according with https://github.com/gitbrent/PptxGenJS/blob/master/demos/node/demo_stream.js
- Code
import pptxgen from 'pptxgenjs';
[...]
const pptx = new pptxgen();
console.log(pptx);
- Result
TypeError: pptxgenjs_1.default is not a constructor
at new PptxAbstractGenerator (/opt/app/src/export/export-pptx/builder/core/generator/pptx-abstract.generator.ts:57:22)
at new PptxV1Generator (/opt/app/src/export/export-pptx/builder/release/v1/pptx-v1.generator.ts:20:9)
at ExportPptxFactoryService.get (/opt/app/src/export/export-pptx/builder/export-pptx-factory.service.ts:43:48)
at ExportPptxService.retrieveExportFile (/opt/app/src/export/export-pptx/service/export-pptx.service.ts:23:45)
at ExportPptxController.<anonymous> (/opt/app/src/export/export-pptx/controller/export-pptx.controller.ts:58:64)
at Generator.next (<anonymous>)
at fulfilled (/opt/app/src/export/export-pptx/controller/export-pptx.controller.ts:16:58)
- Another test inspired fo https://github.com/gitbrent/PptxGenJS/blob/master/demos/react-demo/src/App.js
import PptxGenJS from 'pptxgenjs';
import AlignH = PptxGenJS.AlignH;
const PptxGenJSClazz = require('pptxgenjs');
[...]
const pptx = new PptxGenJSClazz();
const slide = pptx.addSlide();
slide.addText('hero', {
fontSize: 44,
align: AlignH.center,
});
console.log(slide);
Note : for access to AlignH declaration we need to import PptxGenJS. So PptxGenJS namespace and class seen to be have same name. For prevent conflot i rename const PptxGenJS = require('pptxgenjs'); by const PptxGenJSClazz = require('pptxgenjs');
- Result
/opt/app/src/export/export-pptx/builder/core/generator/pptx-abstract.generator.ts:10
import AlignH = PptxGenJS.AlignH;
^
TypeError: Cannot read property 'AlignH' of undefined
at Object.<anonymous> (/opt/app/src/export/export-pptx/builder/core/generator/pptx-abstract.generator.ts:10:27)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Module.m._compile (/opt/app/node_modules/ts-node/src/index.ts:422:23)
at Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Object.require.extensions.(anonymous function) [as .ts] (/opt/app/node_modules/ts-node/src/index.ts:425:12)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/opt/app/src/export/export-pptx/builder/release/v1/pptx-v1.generator.ts:3:1)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Module.m._compile (/opt/app/node_modules/ts-node/src/index.ts:422:23)
at Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Object.require.extensions.(anonymous function) [as .ts] (/opt/app/node_modules/ts-node/src/index.ts:425:12)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/opt/app/src/export/export-pptx/builder/export-pptx-factory.service.ts:4:1)
at Module._compile (internal/modules/cjs/loader.js:778:30)