私は特定のニーズに合わせてchalk
の色をパッチするモジュールを持っています。ここでは、コードです:TypeScriptでオブジェクトをどのように修正しますか?
import { ChalkStyleElement, ChalkStyleMap, styles } from 'chalk';
import escape from './escape';
/**
* Decorate ASCII-colors with shell-specific escapes
*/
let PatchedChalkStyleMap: ChalkStyleMap;
Object.keys(styles).forEach((style: string) => {
PatchedChalkStyleMap[style] = {
close: escape(styles[style].close),
open: escape(styles[style].open),
reset: escape(styles[style].reset),
};
});
私はちょうどchalk
スタイルのすべてを歩くと私の特別なescape
機能でそれらをパッチ上。しかし、これはコンパイルされません。私はこれらのエラーを取得:
src/colors.ts(10,3): error TS7017: Element implicitly has an 'any' type because type 'ChalkStyleMap' has no index signature.
src/colors.ts(11,16): error TS7017: Element implicitly has an 'any' type because type 'ChalkStyleMap' has no index signature.
src/colors.ts(12,15): error TS7017: Element implicitly has an 'any' type because type 'ChalkStyleMap' has no index signature.
src/colors.ts(13,16): error TS7017: Element implicitly has an 'any' type because type 'ChalkStyleMap' has no index signature
また、私は私が私のtsconfig.json
で有効"noImplicitAny"
のオプションを持っていると言うべきです。
ここでタイプを正しく記述するにはどうすればよいですか?暗黙的になることはありませんany
?
なぜですか?私はすでにチョークの入力を使用していて、 'ChalkStyleMap'は既に宣言されています(https://github.com/DefinitelyTyped/DefinitelyTypes/chalk/index.d.ts#L93-L124) – denysdovhan
@ denysdovhanあなたは 'ChalkStyleMap'インターフェースを再宣言していません。必要なインデクサーを追加するだけです。 – Saravana