LoginSignup
0
1

DeepReadonly などを import なしで使用したい。global.d.ts 拡張

Last updated at Posted at 2023-07-24

結論

ts-essentials の DeepReadonly など、よく使用する型は毎回 import するのが手間だし、import が積み重なってしまう。

呼び出し元.ts
import { DeepReadonly } from 'ts-essentials'

type T = DeepReadonly<{ field: string }>

この作業に煩わしさを感じたら global の拡張を検討すると良い。

global.d.ts
import { DeepReadonly } from 'ts-essentials'

declare global {
  type DR<Type> = DeepReadonly<Type>
}
tsconfig.json
{
  "include": ["src/**/*", "global.d.ts"] # src配下にある場合はglobal.d.tsは不要
}
呼び出し元.ts
// import が不要になった
type T = DR<{ field: string }>

ts-essentials

0
1
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
0
1