3
4

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 5 years have passed since last update.

TypeScript で export する関数の引数に分割代入を使うと .d.ts が壊れる

Posted at

問題

  • TypeScript v1.8.7

次のようなファイルで tsc は問題なく成功するが、破損した *.d.ts を生成し、利用時に Error によりコンパイルできない。

index.ts
const f = ({ k }: { k: any }) => k;
export { f };
index.d.ts
declare const f: ({ k }: {
    k: any;
}) => any;
export { f };

エラーメッセージ:

')' expected.

コンパイラオプション:

  "compilerOptions": {
    "declaration": true,
    "module": "commonjs",
    "moduleResolution": "node",
    "noImplicitAny": false,
    "noImplicitReturns": true,
    "outDir": ".tmp/",
    "rootDir": ".",
    "sourceMap": false,
    "target": "es5"
  }

解決策

export する関数の引数では分割代入 (destructuring assignment) を使わない。

index.ts
const f = (options: { k: any }) => {
  const { k } = options;
  return k;
};
export { f };
index.d.ts
declare const f: (options: {
    k: any;
}) => any;
export { f };

おそらく引数に適当な名前をつけることができないのだろう。

3
4
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
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?