1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

TypeScriptでdocxを実行するとcreator: (_a = options2.creator) != null ? _a : "Un-named"エラーが出る

Posted at

はじめに

TypeScriptでDocxを実行する際にあまりネットに情報がなかったので躓いたところまとめています

問題

以下のコードを実行したところエラーが発生しました


```ts
function createDocument(articles: Article[]) {
  const doc = new Document();
      creator: (_a = options2.creator) != null ? _a : "Un-named",
                              ^
TypeError: Cannot read properties of undefined (reading 'creator')
    at new File (/home/watanabejin/workspace/shun-news/node_modules/docx/build/index.cjs:15434:31)
    at createDocument (/home/watanabejin/workspace/shun-news/src/index.ts:30:15)
    at Object.<anonymous> (/home/watanabejin/workspace/shun-news/src/index.ts:58:13)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)
    at Module.m._compile (/home/watanabejin/.npm/_npx/1bf7c3c15bf47d04/node_modules/ts-node/src/index.ts:1618:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Object.require.extensions.<computed> [as .ts] (/home/watanabejin/.npm/_npx/1bf7c3c15bf47d04/node_modules/ts-node/src/index.ts:1621:12)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Function.Module._load (node:internal/modules/cjs/loader:958:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:169:29)

解決方法

以下のようにすることで実行できるようになりました

function createDocument(articles: Article[]) {
  const doc = new Document({
    sections: [],
  });

おわりに

型エラーは以前起きていますが実行できて一安心です

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?