index.d.cts 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116
  1. /**
  2. Returns a boolean for whether the given type is `any`.
  3. @link https://stackoverflow.com/a/49928360/1490091
  4. Useful in type utilities, such as disallowing `any`s to be passed to a function.
  5. @example
  6. ```
  7. import type {IsAny} from 'type-fest';
  8. const typedObject = {a: 1, b: 2} as const;
  9. const anyObject: any = {a: 1, b: 2};
  10. function get<O extends (IsAny<O> extends true ? {} : Record<string, number>), K extends keyof O = keyof O>(obj: O, key: K) {
  11. return obj[key];
  12. }
  13. const typedA = get(typedObject, 'a');
  14. //=> 1
  15. const anyA = get(anyObject, 'a');
  16. //=> any
  17. ```
  18. @category Type Guard
  19. @category Utilities
  20. */
  21. type IsAny<T> = 0 extends 1 & NoInfer<T> ? true : false;
  22. /**
  23. Returns a boolean for whether the given key is an optional key of type.
  24. This is useful when writing utility types or schema validators that need to differentiate `optional` keys.
  25. @example
  26. ```
  27. import type {IsOptionalKeyOf} from 'type-fest';
  28. interface User {
  29. name: string;
  30. surname: string;
  31. luckyNumber?: number;
  32. }
  33. interface Admin {
  34. name: string;
  35. surname?: string;
  36. }
  37. type T1 = IsOptionalKeyOf<User, 'luckyNumber'>;
  38. //=> true
  39. type T2 = IsOptionalKeyOf<User, 'name'>;
  40. //=> false
  41. type T3 = IsOptionalKeyOf<User, 'name' | 'luckyNumber'>;
  42. //=> boolean
  43. type T4 = IsOptionalKeyOf<User | Admin, 'name'>;
  44. //=> false
  45. type T5 = IsOptionalKeyOf<User | Admin, 'surname'>;
  46. //=> boolean
  47. ```
  48. @category Type Guard
  49. @category Utilities
  50. */
  51. type IsOptionalKeyOf<Type extends object, Key extends keyof Type> =
  52. IsAny<Type | Key> extends true ? never
  53. : Key extends keyof Type
  54. ? Type extends Record<Key, Type[Key]>
  55. ? false
  56. : true
  57. : false;
  58. /**
  59. Extract all optional keys from the given type.
  60. This is useful when you want to create a new type that contains different type values for the optional keys only.
  61. @example
  62. ```
  63. import type {OptionalKeysOf, Except} from 'type-fest';
  64. interface User {
  65. name: string;
  66. surname: string;
  67. luckyNumber?: number;
  68. }
  69. const REMOVE_FIELD = Symbol('remove field symbol');
  70. type UpdateOperation<Entity extends object> = Except<Partial<Entity>, OptionalKeysOf<Entity>> & {
  71. [Key in OptionalKeysOf<Entity>]?: Entity[Key] | typeof REMOVE_FIELD;
  72. };
  73. const update1: UpdateOperation<User> = {
  74. name: 'Alice'
  75. };
  76. const update2: UpdateOperation<User> = {
  77. name: 'Bob',
  78. luckyNumber: REMOVE_FIELD
  79. };
  80. ```
  81. @category Utilities
  82. */
  83. type OptionalKeysOf<Type extends object> =
  84. Type extends unknown // For distributing `Type`
  85. ? (keyof {[Key in keyof Type as
  86. IsOptionalKeyOf<Type, Key> extends false
  87. ? never
  88. : Key
  89. ]: never
  90. }) & keyof Type // Intersect with `keyof Type` to ensure result of `OptionalKeysOf<Type>` is always assignable to `keyof Type`
  91. : never; // Should never happen
  92. /**
  93. Extract all required keys from the given type.
  94. This is useful when you want to create a new type that contains different type values for the required keys only or use the list of keys for validation purposes, etc...
  95. @example
  96. ```
  97. import type {RequiredKeysOf} from 'type-fest';
  98. declare function createValidation<Entity extends object, Key extends RequiredKeysOf<Entity> = RequiredKeysOf<Entity>>(field: Key, validator: (value: Entity[Key]) => boolean): ValidatorFn;
  99. interface User {
  100. name: string;
  101. surname: string;
  102. luckyNumber?: number;
  103. }
  104. const validator1 = createValidation<User>('name', value => value.length < 25);
  105. const validator2 = createValidation<User>('surname', value => value.length < 25);
  106. ```
  107. @category Utilities
  108. */
  109. type RequiredKeysOf<Type extends object> =
  110. Type extends unknown // For distributing `Type`
  111. ? Exclude<keyof Type, OptionalKeysOf<Type>>
  112. : never; // Should never happen
  113. /**
  114. Returns a boolean for whether the given type is `never`.
  115. @link https://github.com/microsoft/TypeScript/issues/31751#issuecomment-498526919
  116. @link https://stackoverflow.com/a/53984913/10292952
  117. @link https://www.zhenghao.io/posts/ts-never
  118. Useful in type utilities, such as checking if something does not occur.
  119. @example
  120. ```
  121. import type {IsNever, And} from 'type-fest';
  122. // https://github.com/andnp/SimplyTyped/blob/master/src/types/strings.ts
  123. type AreStringsEqual<A extends string, B extends string> =
  124. And<
  125. IsNever<Exclude<A, B>> extends true ? true : false,
  126. IsNever<Exclude<B, A>> extends true ? true : false
  127. >;
  128. type EndIfEqual<I extends string, O extends string> =
  129. AreStringsEqual<I, O> extends true
  130. ? never
  131. : void;
  132. function endIfEqual<I extends string, O extends string>(input: I, output: O): EndIfEqual<I, O> {
  133. if (input === output) {
  134. process.exit(0);
  135. }
  136. }
  137. endIfEqual('abc', 'abc');
  138. //=> never
  139. endIfEqual('abc', '123');
  140. //=> void
  141. ```
  142. @category Type Guard
  143. @category Utilities
  144. */
  145. type IsNever<T> = [T] extends [never] ? true : false;
  146. /**
  147. An if-else-like type that resolves depending on whether the given `boolean` type is `true` or `false`.
  148. Use-cases:
  149. - You can use this in combination with `Is*` types to create an if-else-like experience. For example, `If<IsAny<any>, 'is any', 'not any'>`.
  150. Note:
  151. - Returns a union of if branch and else branch if the given type is `boolean` or `any`. For example, `If<boolean, 'Y', 'N'>` will return `'Y' | 'N'`.
  152. - Returns the else branch if the given type is `never`. For example, `If<never, 'Y', 'N'>` will return `'N'`.
  153. @example
  154. ```
  155. import {If} from 'type-fest';
  156. type A = If<true, 'yes', 'no'>;
  157. //=> 'yes'
  158. type B = If<false, 'yes', 'no'>;
  159. //=> 'no'
  160. type C = If<boolean, 'yes', 'no'>;
  161. //=> 'yes' | 'no'
  162. type D = If<any, 'yes', 'no'>;
  163. //=> 'yes' | 'no'
  164. type E = If<never, 'yes', 'no'>;
  165. //=> 'no'
  166. ```
  167. @example
  168. ```
  169. import {If, IsAny, IsNever} from 'type-fest';
  170. type A = If<IsAny<unknown>, 'is any', 'not any'>;
  171. //=> 'not any'
  172. type B = If<IsNever<never>, 'is never', 'not never'>;
  173. //=> 'is never'
  174. ```
  175. @example
  176. ```
  177. import {If, IsEqual} from 'type-fest';
  178. type IfEqual<T, U, IfBranch, ElseBranch> = If<IsEqual<T, U>, IfBranch, ElseBranch>;
  179. type A = IfEqual<string, string, 'equal', 'not equal'>;
  180. //=> 'equal'
  181. type B = IfEqual<string, number, 'equal', 'not equal'>;
  182. //=> 'not equal'
  183. ```
  184. @category Type Guard
  185. @category Utilities
  186. */
  187. type If<Type extends boolean, IfBranch, ElseBranch> =
  188. IsNever<Type> extends true
  189. ? ElseBranch
  190. : Type extends true
  191. ? IfBranch
  192. : ElseBranch;
  193. /**
  194. Useful to flatten the type output to improve type hints shown in editors. And also to transform an interface into a type to aide with assignability.
  195. @example
  196. ```
  197. import type {Simplify} from 'type-fest';
  198. type PositionProps = {
  199. top: number;
  200. left: number;
  201. };
  202. type SizeProps = {
  203. width: number;
  204. height: number;
  205. };
  206. // In your editor, hovering over `Props` will show a flattened object with all the properties.
  207. type Props = Simplify<PositionProps & SizeProps>;
  208. ```
  209. Sometimes it is desired to pass a value as a function argument that has a different type. At first inspection it may seem assignable, and then you discover it is not because the `value`'s type definition was defined as an interface. In the following example, `fn` requires an argument of type `Record<string, unknown>`. If the value is defined as a literal, then it is assignable. And if the `value` is defined as type using the `Simplify` utility the value is assignable. But if the `value` is defined as an interface, it is not assignable because the interface is not sealed and elsewhere a non-string property could be added to the interface.
  210. If the type definition must be an interface (perhaps it was defined in a third-party npm package), then the `value` can be defined as `const value: Simplify<SomeInterface> = ...`. Then `value` will be assignable to the `fn` argument. Or the `value` can be cast as `Simplify<SomeInterface>` if you can't re-declare the `value`.
  211. @example
  212. ```
  213. import type {Simplify} from 'type-fest';
  214. interface SomeInterface {
  215. foo: number;
  216. bar?: string;
  217. baz: number | undefined;
  218. }
  219. type SomeType = {
  220. foo: number;
  221. bar?: string;
  222. baz: number | undefined;
  223. };
  224. const literal = {foo: 123, bar: 'hello', baz: 456};
  225. const someType: SomeType = literal;
  226. const someInterface: SomeInterface = literal;
  227. function fn(object: Record<string, unknown>): void {}
  228. fn(literal); // Good: literal object type is sealed
  229. fn(someType); // Good: type is sealed
  230. fn(someInterface); // Error: Index signature for type 'string' is missing in type 'someInterface'. Because `interface` can be re-opened
  231. fn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface` into a `type`
  232. ```
  233. @link https://github.com/microsoft/TypeScript/issues/15300
  234. @see SimplifyDeep
  235. @category Object
  236. */
  237. type Simplify<T> = {[KeyType in keyof T]: T[KeyType]} & {};
  238. /**
  239. Returns a boolean for whether the two given types are equal.
  240. @link https://github.com/microsoft/TypeScript/issues/27024#issuecomment-421529650
  241. @link https://stackoverflow.com/questions/68961864/how-does-the-equals-work-in-typescript/68963796#68963796
  242. Use-cases:
  243. - If you want to make a conditional branch based on the result of a comparison of two types.
  244. @example
  245. ```
  246. import type {IsEqual} from 'type-fest';
  247. // This type returns a boolean for whether the given array includes the given item.
  248. // `IsEqual` is used to compare the given array at position 0 and the given item and then return true if they are equal.
  249. type Includes<Value extends readonly any[], Item> =
  250. Value extends readonly [Value[0], ...infer rest]
  251. ? IsEqual<Value[0], Item> extends true
  252. ? true
  253. : Includes<rest, Item>
  254. : false;
  255. ```
  256. @category Type Guard
  257. @category Utilities
  258. */
  259. type IsEqual<A, B> =
  260. [A, B] extends [infer AA, infer BB]
  261. ? [AA] extends [never]
  262. ? [BB] extends [never]
  263. ? true
  264. : false
  265. : [BB] extends [never]
  266. ? false
  267. : _IsEqual<AA, BB>
  268. : false;
  269. // This version fails the `equalWrappedTupleIntersectionToBeNeverAndNeverExpanded` test in `test-d/is-equal.ts`.
  270. type _IsEqual<A, B> =
  271. (<G>() => G extends A & G | G ? 1 : 2) extends
  272. (<G>() => G extends B & G | G ? 1 : 2)
  273. ? true
  274. : false;
  275. /**
  276. Omit any index signatures from the given object type, leaving only explicitly defined properties.
  277. This is the counterpart of `PickIndexSignature`.
  278. Use-cases:
  279. - Remove overly permissive signatures from third-party types.
  280. This type was taken from this [StackOverflow answer](https://stackoverflow.com/a/68261113/420747).
  281. It relies on the fact that an empty object (`{}`) is assignable to an object with just an index signature, like `Record<string, unknown>`, but not to an object with explicitly defined keys, like `Record<'foo' | 'bar', unknown>`.
  282. (The actual value type, `unknown`, is irrelevant and could be any type. Only the key type matters.)
  283. ```
  284. const indexed: Record<string, unknown> = {}; // Allowed
  285. const keyed: Record<'foo', unknown> = {}; // Error
  286. // => TS2739: Type '{}' is missing the following properties from type 'Record<"foo" | "bar", unknown>': foo, bar
  287. ```
  288. Instead of causing a type error like the above, you can also use a [conditional type](https://www.typescriptlang.org/docs/handbook/2/conditional-types.html) to test whether a type is assignable to another:
  289. ```
  290. type Indexed = {} extends Record<string, unknown>
  291. ? '✅ `{}` is assignable to `Record<string, unknown>`'
  292. : '❌ `{}` is NOT assignable to `Record<string, unknown>`';
  293. // => '✅ `{}` is assignable to `Record<string, unknown>`'
  294. type Keyed = {} extends Record<'foo' | 'bar', unknown>
  295. ? "✅ `{}` is assignable to `Record<'foo' | 'bar', unknown>`"
  296. : "❌ `{}` is NOT assignable to `Record<'foo' | 'bar', unknown>`";
  297. // => "❌ `{}` is NOT assignable to `Record<'foo' | 'bar', unknown>`"
  298. ```
  299. Using a [mapped type](https://www.typescriptlang.org/docs/handbook/2/mapped-types.html#further-exploration), you can then check for each `KeyType` of `ObjectType`...
  300. ```
  301. import type {OmitIndexSignature} from 'type-fest';
  302. type OmitIndexSignature<ObjectType> = {
  303. [KeyType in keyof ObjectType // Map each key of `ObjectType`...
  304. ]: ObjectType[KeyType]; // ...to its original value, i.e. `OmitIndexSignature<Foo> == Foo`.
  305. };
  306. ```
  307. ...whether an empty object (`{}`) would be assignable to an object with that `KeyType` (`Record<KeyType, unknown>`)...
  308. ```
  309. import type {OmitIndexSignature} from 'type-fest';
  310. type OmitIndexSignature<ObjectType> = {
  311. [KeyType in keyof ObjectType
  312. // Is `{}` assignable to `Record<KeyType, unknown>`?
  313. as {} extends Record<KeyType, unknown>
  314. ? ... // ✅ `{}` is assignable to `Record<KeyType, unknown>`
  315. : ... // ❌ `{}` is NOT assignable to `Record<KeyType, unknown>`
  316. ]: ObjectType[KeyType];
  317. };
  318. ```
  319. If `{}` is assignable, it means that `KeyType` is an index signature and we want to remove it. If it is not assignable, `KeyType` is a "real" key and we want to keep it.
  320. @example
  321. ```
  322. import type {OmitIndexSignature} from 'type-fest';
  323. interface Example {
  324. // These index signatures will be removed.
  325. [x: string]: any
  326. [x: number]: any
  327. [x: symbol]: any
  328. [x: `head-${string}`]: string
  329. [x: `${string}-tail`]: string
  330. [x: `head-${string}-tail`]: string
  331. [x: `${bigint}`]: string
  332. [x: `embedded-${number}`]: string
  333. // These explicitly defined keys will remain.
  334. foo: 'bar';
  335. qux?: 'baz';
  336. }
  337. type ExampleWithoutIndexSignatures = OmitIndexSignature<Example>;
  338. // => { foo: 'bar'; qux?: 'baz' | undefined; }
  339. ```
  340. @see PickIndexSignature
  341. @category Object
  342. */
  343. type OmitIndexSignature<ObjectType> = {
  344. [KeyType in keyof ObjectType as {} extends Record<KeyType, unknown>
  345. ? never
  346. : KeyType]: ObjectType[KeyType];
  347. };
  348. /**
  349. Pick only index signatures from the given object type, leaving out all explicitly defined properties.
  350. This is the counterpart of `OmitIndexSignature`.
  351. @example
  352. ```
  353. import type {PickIndexSignature} from 'type-fest';
  354. declare const symbolKey: unique symbol;
  355. type Example = {
  356. // These index signatures will remain.
  357. [x: string]: unknown;
  358. [x: number]: unknown;
  359. [x: symbol]: unknown;
  360. [x: `head-${string}`]: string;
  361. [x: `${string}-tail`]: string;
  362. [x: `head-${string}-tail`]: string;
  363. [x: `${bigint}`]: string;
  364. [x: `embedded-${number}`]: string;
  365. // These explicitly defined keys will be removed.
  366. ['kebab-case-key']: string;
  367. [symbolKey]: string;
  368. foo: 'bar';
  369. qux?: 'baz';
  370. };
  371. type ExampleIndexSignature = PickIndexSignature<Example>;
  372. // {
  373. // [x: string]: unknown;
  374. // [x: number]: unknown;
  375. // [x: symbol]: unknown;
  376. // [x: `head-${string}`]: string;
  377. // [x: `${string}-tail`]: string;
  378. // [x: `head-${string}-tail`]: string;
  379. // [x: `${bigint}`]: string;
  380. // [x: `embedded-${number}`]: string;
  381. // }
  382. ```
  383. @see OmitIndexSignature
  384. @category Object
  385. */
  386. type PickIndexSignature<ObjectType> = {
  387. [KeyType in keyof ObjectType as {} extends Record<KeyType, unknown>
  388. ? KeyType
  389. : never]: ObjectType[KeyType];
  390. };
  391. // Merges two objects without worrying about index signatures.
  392. type SimpleMerge<Destination, Source> = {
  393. [Key in keyof Destination as Key extends keyof Source ? never : Key]: Destination[Key];
  394. } & Source;
  395. /**
  396. Merge two types into a new type. Keys of the second type overrides keys of the first type.
  397. @example
  398. ```
  399. import type {Merge} from 'type-fest';
  400. interface Foo {
  401. [x: string]: unknown;
  402. [x: number]: unknown;
  403. foo: string;
  404. bar: symbol;
  405. }
  406. type Bar = {
  407. [x: number]: number;
  408. [x: symbol]: unknown;
  409. bar: Date;
  410. baz: boolean;
  411. };
  412. export type FooBar = Merge<Foo, Bar>;
  413. // => {
  414. // [x: string]: unknown;
  415. // [x: number]: number;
  416. // [x: symbol]: unknown;
  417. // foo: string;
  418. // bar: Date;
  419. // baz: boolean;
  420. // }
  421. ```
  422. @category Object
  423. */
  424. type Merge<Destination, Source> =
  425. Simplify<
  426. SimpleMerge<PickIndexSignature<Destination>, PickIndexSignature<Source>>
  427. & SimpleMerge<OmitIndexSignature<Destination>, OmitIndexSignature<Source>>
  428. >;
  429. /**
  430. Merges user specified options with default options.
  431. @example
  432. ```
  433. type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
  434. type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: false};
  435. type SpecifiedOptions = {leavesOnly: true};
  436. type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
  437. //=> {maxRecursionDepth: 10; leavesOnly: true}
  438. ```
  439. @example
  440. ```
  441. // Complains if default values are not provided for optional options
  442. type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
  443. type DefaultPathsOptions = {maxRecursionDepth: 10};
  444. type SpecifiedOptions = {};
  445. type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
  446. // ~~~~~~~~~~~~~~~~~~~
  447. // Property 'leavesOnly' is missing in type 'DefaultPathsOptions' but required in type '{ maxRecursionDepth: number; leavesOnly: boolean; }'.
  448. ```
  449. @example
  450. ```
  451. // Complains if an option's default type does not conform to the expected type
  452. type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
  453. type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: 'no'};
  454. type SpecifiedOptions = {};
  455. type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
  456. // ~~~~~~~~~~~~~~~~~~~
  457. // Types of property 'leavesOnly' are incompatible. Type 'string' is not assignable to type 'boolean'.
  458. ```
  459. @example
  460. ```
  461. // Complains if an option's specified type does not conform to the expected type
  462. type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
  463. type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: false};
  464. type SpecifiedOptions = {leavesOnly: 'yes'};
  465. type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
  466. // ~~~~~~~~~~~~~~~~
  467. // Types of property 'leavesOnly' are incompatible. Type 'string' is not assignable to type 'boolean'.
  468. ```
  469. */
  470. type ApplyDefaultOptions<
  471. Options extends object,
  472. Defaults extends Simplify<Omit<Required<Options>, RequiredKeysOf<Options>> & Partial<Record<RequiredKeysOf<Options>, never>>>,
  473. SpecifiedOptions extends Options,
  474. > =
  475. If<IsAny<SpecifiedOptions>, Defaults,
  476. If<IsNever<SpecifiedOptions>, Defaults,
  477. Simplify<Merge<Defaults, {
  478. [Key in keyof SpecifiedOptions
  479. as Key extends OptionalKeysOf<Options> ? undefined extends SpecifiedOptions[Key] ? never : Key : Key
  480. ]: SpecifiedOptions[Key]
  481. }> & Required<Options>>>>;
  482. /**
  483. Filter out keys from an object.
  484. Returns `never` if `Exclude` is strictly equal to `Key`.
  485. Returns `never` if `Key` extends `Exclude`.
  486. Returns `Key` otherwise.
  487. @example
  488. ```
  489. type Filtered = Filter<'foo', 'foo'>;
  490. //=> never
  491. ```
  492. @example
  493. ```
  494. type Filtered = Filter<'bar', string>;
  495. //=> never
  496. ```
  497. @example
  498. ```
  499. type Filtered = Filter<'bar', 'foo'>;
  500. //=> 'bar'
  501. ```
  502. @see {Except}
  503. */
  504. type Filter<KeyType, ExcludeType> = IsEqual<KeyType, ExcludeType> extends true ? never : (KeyType extends ExcludeType ? never : KeyType);
  505. type ExceptOptions = {
  506. /**
  507. Disallow assigning non-specified properties.
  508. Note that any omitted properties in the resulting type will be present in autocomplete as `undefined`.
  509. @default false
  510. */
  511. requireExactProps?: boolean;
  512. };
  513. type DefaultExceptOptions = {
  514. requireExactProps: false;
  515. };
  516. /**
  517. Create a type from an object type without certain keys.
  518. We recommend setting the `requireExactProps` option to `true`.
  519. This type is a stricter version of [`Omit`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-5.html#the-omit-helper-type). The `Omit` type does not restrict the omitted keys to be keys present on the given type, while `Except` does. The benefits of a stricter type are avoiding typos and allowing the compiler to pick up on rename refactors automatically.
  520. This type was proposed to the TypeScript team, which declined it, saying they prefer that libraries implement stricter versions of the built-in types ([microsoft/TypeScript#30825](https://github.com/microsoft/TypeScript/issues/30825#issuecomment-523668235)).
  521. @example
  522. ```
  523. import type {Except} from 'type-fest';
  524. type Foo = {
  525. a: number;
  526. b: string;
  527. };
  528. type FooWithoutA = Except<Foo, 'a'>;
  529. //=> {b: string}
  530. const fooWithoutA: FooWithoutA = {a: 1, b: '2'};
  531. //=> errors: 'a' does not exist in type '{ b: string; }'
  532. type FooWithoutB = Except<Foo, 'b', {requireExactProps: true}>;
  533. //=> {a: number} & Partial<Record<"b", never>>
  534. const fooWithoutB: FooWithoutB = {a: 1, b: '2'};
  535. //=> errors at 'b': Type 'string' is not assignable to type 'undefined'.
  536. // The `Omit` utility type doesn't work when omitting specific keys from objects containing index signatures.
  537. // Consider the following example:
  538. type UserData = {
  539. [metadata: string]: string;
  540. email: string;
  541. name: string;
  542. role: 'admin' | 'user';
  543. };
  544. // `Omit` clearly doesn't behave as expected in this case:
  545. type PostPayload = Omit<UserData, 'email'>;
  546. //=> type PostPayload = { [x: string]: string; [x: number]: string; }
  547. // In situations like this, `Except` works better.
  548. // It simply removes the `email` key while preserving all the other keys.
  549. type PostPayload = Except<UserData, 'email'>;
  550. //=> type PostPayload = { [x: string]: string; name: string; role: 'admin' | 'user'; }
  551. ```
  552. @category Object
  553. */
  554. type Except<ObjectType, KeysType extends keyof ObjectType, Options extends ExceptOptions = {}> =
  555. _Except<ObjectType, KeysType, ApplyDefaultOptions<ExceptOptions, DefaultExceptOptions, Options>>;
  556. type _Except<ObjectType, KeysType extends keyof ObjectType, Options extends Required<ExceptOptions>> = {
  557. [KeyType in keyof ObjectType as Filter<KeyType, KeysType>]: ObjectType[KeyType];
  558. } & (Options['requireExactProps'] extends true
  559. ? Partial<Record<KeysType, never>>
  560. : {});
  561. declare namespace TsConfigJson {
  562. namespace CompilerOptions {
  563. type JSX =
  564. | 'preserve'
  565. | 'react'
  566. | 'react-jsx'
  567. | 'react-jsxdev'
  568. | 'react-native';
  569. type Module =
  570. | 'CommonJS'
  571. | 'AMD'
  572. | 'System'
  573. | 'UMD'
  574. | 'ES6'
  575. | 'ES2015'
  576. | 'ES2020'
  577. | 'ES2022'
  578. | 'ESNext'
  579. | 'Node16'
  580. | 'Node18'
  581. | 'Node20'
  582. | 'NodeNext'
  583. | 'Preserve'
  584. | 'None'
  585. // Lowercase alternatives
  586. | 'commonjs'
  587. | 'amd'
  588. | 'system'
  589. | 'umd'
  590. | 'es6'
  591. | 'es2015'
  592. | 'es2020'
  593. | 'es2022'
  594. | 'esnext'
  595. | 'node16'
  596. | 'node18'
  597. | 'node20'
  598. | 'nodenext'
  599. | 'preserve'
  600. | 'none';
  601. type NewLine =
  602. | 'CRLF'
  603. | 'LF'
  604. // Lowercase alternatives
  605. | 'crlf'
  606. | 'lf';
  607. type Target =
  608. | 'ES3'
  609. | 'ES5'
  610. | 'ES6'
  611. | 'ES2015'
  612. | 'ES2016'
  613. | 'ES2017'
  614. | 'ES2018'
  615. | 'ES2019'
  616. | 'ES2020'
  617. | 'ES2021'
  618. | 'ES2022'
  619. | 'ES2023'
  620. | 'ES2024'
  621. | 'ESNext'
  622. // Lowercase alternatives
  623. | 'es3'
  624. | 'es5'
  625. | 'es6'
  626. | 'es2015'
  627. | 'es2016'
  628. | 'es2017'
  629. | 'es2018'
  630. | 'es2019'
  631. | 'es2020'
  632. | 'es2021'
  633. | 'es2022'
  634. | 'es2023'
  635. | 'es2024'
  636. | 'esnext';
  637. type Lib =
  638. | 'ES5'
  639. | 'ES6'
  640. | 'ES7'
  641. | 'ES2015'
  642. | 'ES2015.Collection'
  643. | 'ES2015.Core'
  644. | 'ES2015.Generator'
  645. | 'ES2015.Iterable'
  646. | 'ES2015.Promise'
  647. | 'ES2015.Proxy'
  648. | 'ES2015.Reflect'
  649. | 'ES2015.Symbol.WellKnown'
  650. | 'ES2015.Symbol'
  651. | 'ES2016'
  652. | 'ES2016.Array.Include'
  653. | 'ES2017'
  654. | 'ES2017.ArrayBuffer'
  655. | 'ES2017.Date'
  656. | 'ES2017.Intl'
  657. | 'ES2017.Object'
  658. | 'ES2017.SharedMemory'
  659. | 'ES2017.String'
  660. | 'ES2017.TypedArrays'
  661. | 'ES2018'
  662. | 'ES2018.AsyncGenerator'
  663. | 'ES2018.AsyncIterable'
  664. | 'ES2018.Intl'
  665. | 'ES2018.Promise'
  666. | 'ES2018.Regexp'
  667. | 'ES2019'
  668. | 'ES2019.Array'
  669. | 'ES2019.Intl'
  670. | 'ES2019.Object'
  671. | 'ES2019.String'
  672. | 'ES2019.Symbol'
  673. | 'ES2020'
  674. | 'ES2020.BigInt'
  675. | 'ES2020.Date'
  676. | 'ES2020.Intl'
  677. | 'ES2020.Number'
  678. | 'ES2020.Promise'
  679. | 'ES2020.SharedMemory'
  680. | 'ES2020.String'
  681. | 'ES2020.Symbol.WellKnown'
  682. | 'ES2021'
  683. | 'ES2021.Intl'
  684. | 'ES2021.Promise'
  685. | 'ES2021.String'
  686. | 'ES2021.WeakRef'
  687. | 'ES2022'
  688. | 'ES2022.Array'
  689. | 'ES2022.Error'
  690. | 'ES2022.Intl'
  691. | 'ES2022.Object'
  692. | 'ES2022.RegExp'
  693. | 'ES2022.SharedMemory'
  694. | 'ES2022.String'
  695. | 'ES2023'
  696. | 'ES2023.Array'
  697. | 'ES2023.Collection'
  698. | 'ES2023.Intl'
  699. | 'ES2024'
  700. | 'ES2024.ArrayBuffer'
  701. | 'ES2024.Collection'
  702. | 'ES2024.Object'
  703. | 'ES2024.Promise'
  704. | 'ES2024.Regexp'
  705. | 'ES2024.SharedMemory'
  706. | 'ES2024.String'
  707. | 'ESNext'
  708. | 'ESNext.Array'
  709. | 'ESNext.AsyncIterable'
  710. | 'ESNext.BigInt'
  711. | 'ESNext.Collection'
  712. | 'ESNext.Decorators'
  713. | 'ESNext.Disposable'
  714. | 'ESNext.Error'
  715. | 'ESNext.Intl'
  716. | 'ESNext.Iterator'
  717. | 'ESNext.Object'
  718. | 'ESNext.Promise'
  719. | 'ESNext.Regexp'
  720. | 'ESNext.String'
  721. | 'ESNext.Symbol'
  722. | 'ESNext.WeakRef'
  723. | 'DOM'
  724. | 'DOM.AsyncIterable'
  725. | 'DOM.Iterable'
  726. | 'Decorators'
  727. | 'Decorators.Legacy'
  728. | 'ScriptHost'
  729. | 'WebWorker'
  730. | 'WebWorker.AsyncIterable'
  731. | 'WebWorker.ImportScripts'
  732. | 'WebWorker.Iterable'
  733. // Lowercase alternatives
  734. | 'es5'
  735. | 'es6'
  736. | 'es7'
  737. | 'es2015'
  738. | 'es2015.collection'
  739. | 'es2015.core'
  740. | 'es2015.generator'
  741. | 'es2015.iterable'
  742. | 'es2015.promise'
  743. | 'es2015.proxy'
  744. | 'es2015.reflect'
  745. | 'es2015.symbol.wellknown'
  746. | 'es2015.symbol'
  747. | 'es2016'
  748. | 'es2016.array.include'
  749. | 'es2017'
  750. | 'es2017.arraybuffer'
  751. | 'es2017.date'
  752. | 'es2017.intl'
  753. | 'es2017.object'
  754. | 'es2017.sharedmemory'
  755. | 'es2017.string'
  756. | 'es2017.typedarrays'
  757. | 'es2018'
  758. | 'es2018.asyncgenerator'
  759. | 'es2018.asynciterable'
  760. | 'es2018.intl'
  761. | 'es2018.promise'
  762. | 'es2018.regexp'
  763. | 'es2019'
  764. | 'es2019.array'
  765. | 'es2019.intl'
  766. | 'es2019.object'
  767. | 'es2019.string'
  768. | 'es2019.symbol'
  769. | 'es2020'
  770. | 'es2020.bigint'
  771. | 'es2020.date'
  772. | 'es2020.intl'
  773. | 'es2020.number'
  774. | 'es2020.promise'
  775. | 'es2020.sharedmemory'
  776. | 'es2020.string'
  777. | 'es2020.symbol.wellknown'
  778. | 'es2021'
  779. | 'es2021.intl'
  780. | 'es2021.promise'
  781. | 'es2021.string'
  782. | 'es2021.weakref'
  783. | 'es2022'
  784. | 'es2022.array'
  785. | 'es2022.error'
  786. | 'es2022.intl'
  787. | 'es2022.object'
  788. | 'es2022.regexp'
  789. | 'es2022.sharedmemory'
  790. | 'es2022.string'
  791. | 'es2023'
  792. | 'es2023.array'
  793. | 'es2023.collection'
  794. | 'es2023.intl'
  795. | 'es2024'
  796. | 'es2024.arraybuffer'
  797. | 'es2024.collection'
  798. | 'es2024.object'
  799. | 'es2024.promise'
  800. | 'es2024.regexp'
  801. | 'es2024.sharedmemory'
  802. | 'es2024.string'
  803. | 'esnext'
  804. | 'esnext.array'
  805. | 'esnext.asynciterable'
  806. | 'esnext.bigint'
  807. | 'esnext.collection'
  808. | 'esnext.decorators'
  809. | 'esnext.disposable'
  810. | 'esnext.error'
  811. | 'esnext.intl'
  812. | 'esnext.iterator'
  813. | 'esnext.object'
  814. | 'esnext.promise'
  815. | 'esnext.regexp'
  816. | 'esnext.string'
  817. | 'esnext.symbol'
  818. | 'esnext.weakref'
  819. | 'dom'
  820. | 'dom.asynciterable'
  821. | 'dom.iterable'
  822. | 'decorators'
  823. | 'decorators.legacy'
  824. | 'scripthost'
  825. | 'webworker'
  826. | 'webworker.asynciterable'
  827. | 'webworker.importscripts'
  828. | 'webworker.iterable';
  829. type Plugin = {
  830. /**
  831. Plugin name.
  832. */
  833. name: string;
  834. };
  835. type ImportsNotUsedAsValues =
  836. | 'remove'
  837. | 'preserve'
  838. | 'error';
  839. type FallbackPolling =
  840. | 'fixedPollingInterval'
  841. | 'priorityPollingInterval'
  842. | 'dynamicPriorityPolling'
  843. | 'fixedInterval'
  844. | 'priorityInterval'
  845. | 'dynamicPriority'
  846. | 'fixedChunkSize';
  847. type WatchDirectory =
  848. | 'useFsEvents'
  849. | 'fixedPollingInterval'
  850. | 'dynamicPriorityPolling'
  851. | 'fixedChunkSizePolling';
  852. type WatchFile =
  853. | 'fixedPollingInterval'
  854. | 'priorityPollingInterval'
  855. | 'dynamicPriorityPolling'
  856. | 'useFsEvents'
  857. | 'useFsEventsOnParentDirectory'
  858. | 'fixedChunkSizePolling';
  859. type ModuleResolution =
  860. | 'classic'
  861. | 'node'
  862. | 'node10'
  863. | 'node16'
  864. | 'nodenext'
  865. | 'bundler'
  866. // Pascal-cased alternatives
  867. | 'Classic'
  868. | 'Node'
  869. | 'Node10'
  870. | 'Node16'
  871. | 'NodeNext'
  872. | 'Bundler';
  873. type ModuleDetection =
  874. | 'auto'
  875. | 'legacy'
  876. | 'force';
  877. type IgnoreDeprecations = '5.0';
  878. }
  879. type CompilerOptions = {
  880. /**
  881. The character set of the input files.
  882. @default 'utf8'
  883. @deprecated This option will be removed in TypeScript 5.5.
  884. */
  885. charset?: string;
  886. /**
  887. Enables building for project references.
  888. @default true
  889. */
  890. composite?: boolean;
  891. /**
  892. Generates corresponding d.ts files.
  893. @default false
  894. */
  895. declaration?: boolean;
  896. /**
  897. Specify output directory for generated declaration files.
  898. */
  899. declarationDir?: string;
  900. /**
  901. Show diagnostic information.
  902. @default false
  903. */
  904. diagnostics?: boolean;
  905. /**
  906. Reduce the number of projects loaded automatically by TypeScript.
  907. @default false
  908. */
  909. disableReferencedProjectLoad?: boolean;
  910. /**
  911. Enforces using indexed accessors for keys declared using an indexed type.
  912. @default false
  913. */
  914. noPropertyAccessFromIndexSignature?: boolean;
  915. /**
  916. Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files.
  917. @default false
  918. */
  919. emitBOM?: boolean;
  920. /**
  921. Only emit `.d.ts` declaration files.
  922. @default false
  923. */
  924. emitDeclarationOnly?: boolean;
  925. /**
  926. Differentiate between undefined and not present when type checking.
  927. @default false
  928. */
  929. exactOptionalPropertyTypes?: boolean;
  930. /**
  931. Enable incremental compilation.
  932. @default `composite`
  933. */
  934. incremental?: boolean;
  935. /**
  936. Specify file to store incremental compilation information.
  937. @default '.tsbuildinfo'
  938. */
  939. tsBuildInfoFile?: string;
  940. /**
  941. Emit a single file with source maps instead of having a separate file.
  942. @default false
  943. */
  944. inlineSourceMap?: boolean;
  945. /**
  946. Emit the source alongside the sourcemaps within a single file.
  947. Requires `--inlineSourceMap` to be set.
  948. @default false
  949. */
  950. inlineSources?: boolean;
  951. /**
  952. Specify what JSX code is generated.
  953. @default 'preserve'
  954. */
  955. jsx?: CompilerOptions.JSX;
  956. /**
  957. Specifies the object invoked for `createElement` and `__spread` when targeting `'react'` JSX emit.
  958. @default 'React'
  959. */
  960. reactNamespace?: string;
  961. /**
  962. Specify the JSX factory function to use when targeting React JSX emit, e.g. `React.createElement` or `h`.
  963. @default 'React.createElement'
  964. */
  965. jsxFactory?: string;
  966. /**
  967. Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'.
  968. @default 'React.Fragment'
  969. */
  970. jsxFragmentFactory?: string;
  971. /**
  972. Specify module specifier used to import the JSX factory functions when using `jsx: react-jsx*`.
  973. @default 'react'
  974. */
  975. jsxImportSource?: string;
  976. /**
  977. Print names of files part of the compilation.
  978. @default false
  979. */
  980. listFiles?: boolean;
  981. /**
  982. Specifies the location where debugger should locate map files instead of generated locations.
  983. */
  984. mapRoot?: string;
  985. /**
  986. Specify module code generation: 'None', 'CommonJS', 'AMD', 'System', 'UMD', 'ES6', 'ES2015' or 'ESNext'. Only 'AMD' and 'System' can be used in conjunction with `--outFile`. 'ES6' and 'ES2015' values may be used when targeting 'ES5' or lower.
  987. @default ['ES3', 'ES5'].includes(target) ? 'CommonJS' : 'ES6'
  988. */
  989. module?: CompilerOptions.Module;
  990. /**
  991. Specifies module resolution strategy: 'node' (Node) or 'classic' (TypeScript pre 1.6).
  992. @default ['AMD', 'System', 'ES6'].includes(module) ? 'classic' : 'node'
  993. */
  994. moduleResolution?: CompilerOptions.ModuleResolution;
  995. /**
  996. Specifies the end of line sequence to be used when emitting files: 'crlf' (Windows) or 'lf' (Unix).
  997. @default 'LF'
  998. */
  999. newLine?: CompilerOptions.NewLine;
  1000. /**
  1001. Disable full type checking (only critical parse and emit errors will be reported).
  1002. @default false
  1003. */
  1004. noCheck?: boolean;
  1005. /**
  1006. Do not emit output.
  1007. @default false
  1008. */
  1009. noEmit?: boolean;
  1010. /**
  1011. Do not generate custom helper functions like `__extends` in compiled output.
  1012. @default false
  1013. */
  1014. noEmitHelpers?: boolean;
  1015. /**
  1016. Do not emit outputs if any type checking errors were reported.
  1017. @default false
  1018. */
  1019. noEmitOnError?: boolean;
  1020. /**
  1021. Warn on expressions and declarations with an implied 'any' type.
  1022. @default false
  1023. */
  1024. noImplicitAny?: boolean;
  1025. /**
  1026. Raise error on 'this' expressions with an implied any type.
  1027. @default false
  1028. */
  1029. noImplicitThis?: boolean;
  1030. /**
  1031. Report errors on unused locals.
  1032. @default false
  1033. */
  1034. noUnusedLocals?: boolean;
  1035. /**
  1036. Report errors on unused parameters.
  1037. @default false
  1038. */
  1039. noUnusedParameters?: boolean;
  1040. /**
  1041. Do not include the default library file (lib.d.ts).
  1042. @default false
  1043. */
  1044. noLib?: boolean;
  1045. /**
  1046. Do not add triple-slash references or module import targets to the list of compiled files.
  1047. @default false
  1048. */
  1049. noResolve?: boolean;
  1050. /**
  1051. Disable strict checking of generic signatures in function types.
  1052. @default false
  1053. @deprecated This option will be removed in TypeScript 5.5.
  1054. */
  1055. noStrictGenericChecks?: boolean;
  1056. /**
  1057. @deprecated use `skipLibCheck` instead.
  1058. */
  1059. skipDefaultLibCheck?: boolean;
  1060. /**
  1061. Skip type checking of declaration files.
  1062. @default false
  1063. */
  1064. skipLibCheck?: boolean;
  1065. /**
  1066. Concatenate and emit output to single file.
  1067. */
  1068. outFile?: string;
  1069. /**
  1070. Redirect output structure to the directory.
  1071. */
  1072. outDir?: string;
  1073. /**
  1074. Do not erase const enum declarations in generated code.
  1075. @default false
  1076. */
  1077. preserveConstEnums?: boolean;
  1078. /**
  1079. Do not resolve symlinks to their real path; treat a symlinked file like a real one.
  1080. @default false
  1081. */
  1082. preserveSymlinks?: boolean;
  1083. /**
  1084. Keep outdated console output in watch mode instead of clearing the screen.
  1085. @default false
  1086. */
  1087. preserveWatchOutput?: boolean;
  1088. /**
  1089. Stylize errors and messages using color and context (experimental).
  1090. @default true // Unless piping to another program or redirecting output to a file.
  1091. */
  1092. pretty?: boolean;
  1093. /**
  1094. Do not emit comments to output.
  1095. @default false
  1096. */
  1097. removeComments?: boolean;
  1098. /**
  1099. Rewrite '.ts', '.tsx', '.mts', and '.cts' file extensions in relative import paths to their JavaScript equivalent in output files.
  1100. @default false
  1101. */
  1102. rewriteRelativeImportExtensions?: boolean;
  1103. /**
  1104. Specifies the root directory of input files.
  1105. Use to control the output directory structure with `--outDir`.
  1106. */
  1107. rootDir?: string;
  1108. /**
  1109. Unconditionally emit imports for unresolved files.
  1110. @default false
  1111. */
  1112. isolatedModules?: boolean;
  1113. /**
  1114. Require sufficient annotation on exports so other tools can trivially generate declaration files.
  1115. @default false
  1116. */
  1117. isolatedDeclarations?: boolean;
  1118. /**
  1119. Generates corresponding '.map' file.
  1120. @default false
  1121. */
  1122. sourceMap?: boolean;
  1123. /**
  1124. Specifies the location where debugger should locate TypeScript files instead of source locations.
  1125. */
  1126. sourceRoot?: string;
  1127. /**
  1128. Suppress excess property checks for object literals.
  1129. @default false
  1130. @deprecated This option will be removed in TypeScript 5.5.
  1131. */
  1132. suppressExcessPropertyErrors?: boolean;
  1133. /**
  1134. Suppress noImplicitAny errors for indexing objects lacking index signatures.
  1135. @default false
  1136. @deprecated This option will be removed in TypeScript 5.5.
  1137. */
  1138. suppressImplicitAnyIndexErrors?: boolean;
  1139. /**
  1140. Do not emit declarations for code that has an `@internal` annotation.
  1141. */
  1142. stripInternal?: boolean;
  1143. /**
  1144. Specify ECMAScript target version.
  1145. @default 'es3'
  1146. */
  1147. target?: CompilerOptions.Target;
  1148. /**
  1149. Default catch clause variables as `unknown` instead of `any`.
  1150. @default false
  1151. */
  1152. useUnknownInCatchVariables?: boolean;
  1153. /**
  1154. Watch input files.
  1155. @default false
  1156. @deprecated Use watchOptions instead.
  1157. */
  1158. watch?: boolean;
  1159. /**
  1160. Specify the polling strategy to use when the system runs out of or doesn't support native file watchers.
  1161. @deprecated Use watchOptions.fallbackPolling instead.
  1162. */
  1163. fallbackPolling?: CompilerOptions.FallbackPolling;
  1164. /**
  1165. Specify the strategy for watching directories under systems that lack recursive file-watching functionality.
  1166. @default 'useFsEvents'
  1167. @deprecated Use watchOptions.watchDirectory instead.
  1168. */
  1169. watchDirectory?: CompilerOptions.WatchDirectory;
  1170. /**
  1171. Specify the strategy for watching individual files.
  1172. @default 'useFsEvents'
  1173. @deprecated Use watchOptions.watchFile instead.
  1174. */
  1175. watchFile?: CompilerOptions.WatchFile;
  1176. /**
  1177. Enables experimental support for ES7 decorators.
  1178. @default false
  1179. */
  1180. experimentalDecorators?: boolean;
  1181. /**
  1182. Emit design-type metadata for decorated declarations in source.
  1183. @default false
  1184. */
  1185. emitDecoratorMetadata?: boolean;
  1186. /**
  1187. Do not report errors on unused labels.
  1188. @default false
  1189. */
  1190. allowUnusedLabels?: boolean;
  1191. /**
  1192. Report error when not all code paths in function return a value.
  1193. @default false
  1194. */
  1195. noImplicitReturns?: boolean;
  1196. /**
  1197. Add `undefined` to a type when accessed using an index.
  1198. @default false
  1199. */
  1200. noUncheckedIndexedAccess?: boolean;
  1201. /**
  1202. Report error if failed to find a source file for a side effect import.
  1203. @default false
  1204. */
  1205. noUncheckedSideEffectImports?: boolean;
  1206. /**
  1207. Report errors for fallthrough cases in switch statement.
  1208. @default false
  1209. */
  1210. noFallthroughCasesInSwitch?: boolean;
  1211. /**
  1212. Ensure overriding members in derived classes are marked with an override modifier.
  1213. @default false
  1214. */
  1215. noImplicitOverride?: boolean;
  1216. /**
  1217. Do not report errors on unreachable code.
  1218. @default false
  1219. */
  1220. allowUnreachableCode?: boolean;
  1221. /**
  1222. Disallow inconsistently-cased references to the same file.
  1223. @default true
  1224. */
  1225. forceConsistentCasingInFileNames?: boolean;
  1226. /**
  1227. Emit a v8 CPU profile of the compiler run for debugging.
  1228. @default 'profile.cpuprofile'
  1229. */
  1230. generateCpuProfile?: string;
  1231. /**
  1232. Generates an event trace and a list of types.
  1233. */
  1234. generateTrace?: boolean;
  1235. /**
  1236. Base directory to resolve non-relative module names.
  1237. */
  1238. baseUrl?: string;
  1239. /**
  1240. Specify path mapping to be computed relative to baseUrl option.
  1241. */
  1242. paths?: Record<string, string[]>;
  1243. /**
  1244. List of TypeScript language server plugins to load.
  1245. */
  1246. plugins?: CompilerOptions.Plugin[];
  1247. /**
  1248. Specify list of root directories to be used when resolving modules.
  1249. */
  1250. rootDirs?: string[];
  1251. /**
  1252. Specify list of directories for type definition files to be included.
  1253. */
  1254. typeRoots?: string[];
  1255. /**
  1256. Type declaration files to be included in compilation.
  1257. */
  1258. types?: string[];
  1259. /**
  1260. Enable tracing of the name resolution process.
  1261. @default false
  1262. */
  1263. traceResolution?: boolean;
  1264. /**
  1265. Allow javascript files to be compiled.
  1266. @default false
  1267. */
  1268. allowJs?: boolean;
  1269. /**
  1270. Do not truncate error messages.
  1271. @default false
  1272. */
  1273. noErrorTruncation?: boolean;
  1274. /**
  1275. Allow default imports from modules with no default export. This does not affect code emit, just typechecking.
  1276. @default module === 'system' || esModuleInterop
  1277. */
  1278. allowSyntheticDefaultImports?: boolean;
  1279. /**
  1280. Do not emit `'use strict'` directives in module output.
  1281. @default false
  1282. @deprecated This option will be removed in TypeScript 5.5.
  1283. */
  1284. noImplicitUseStrict?: boolean;
  1285. /**
  1286. Enable to list all emitted files.
  1287. @default false
  1288. */
  1289. listEmittedFiles?: boolean;
  1290. /**
  1291. Disable size limit for JavaScript project.
  1292. @default false
  1293. */
  1294. disableSizeLimit?: boolean;
  1295. /**
  1296. List of library files to be included in the compilation.
  1297. */
  1298. lib?: CompilerOptions.Lib[];
  1299. /**
  1300. Enable strict null checks.
  1301. @default false
  1302. */
  1303. strictNullChecks?: boolean;
  1304. /**
  1305. The maximum dependency depth to search under `node_modules` and load JavaScript files. Only applicable with `--allowJs`.
  1306. @default 0
  1307. */
  1308. maxNodeModuleJsDepth?: number;
  1309. /**
  1310. Import emit helpers (e.g. `__extends`, `__rest`, etc..) from tslib.
  1311. @default false
  1312. */
  1313. importHelpers?: boolean;
  1314. /**
  1315. Specify emit/checking behavior for imports that are only used for types.
  1316. @default 'remove'
  1317. @deprecated Use `verbatimModuleSyntax` instead.
  1318. */
  1319. importsNotUsedAsValues?: CompilerOptions.ImportsNotUsedAsValues;
  1320. /**
  1321. Parse in strict mode and emit `'use strict'` for each source file.
  1322. @default false
  1323. */
  1324. alwaysStrict?: boolean;
  1325. /**
  1326. Enable all strict type checking options.
  1327. @default false
  1328. */
  1329. strict?: boolean;
  1330. /**
  1331. Enable stricter checking of of the `bind`, `call`, and `apply` methods on functions.
  1332. @default false
  1333. */
  1334. strictBindCallApply?: boolean;
  1335. /**
  1336. Provide full support for iterables in `for-of`, spread, and destructuring when targeting `ES5` or `ES3`.
  1337. @default false
  1338. */
  1339. downlevelIteration?: boolean;
  1340. /**
  1341. Report errors in `.js` files.
  1342. @default false
  1343. */
  1344. checkJs?: boolean;
  1345. /**
  1346. Built-in iterators are instantiated with a `TReturn` type of undefined instead of `any`.
  1347. @default false
  1348. */
  1349. strictBuiltinIteratorReturn?: boolean;
  1350. /**
  1351. Disable bivariant parameter checking for function types.
  1352. @default false
  1353. */
  1354. strictFunctionTypes?: boolean;
  1355. /**
  1356. Ensure non-undefined class properties are initialized in the constructor.
  1357. @default false
  1358. */
  1359. strictPropertyInitialization?: boolean;
  1360. /**
  1361. Emit `__importStar` and `__importDefault` helpers for runtime Babel ecosystem compatibility and enable `--allowSyntheticDefaultImports` for typesystem compatibility.
  1362. @default false
  1363. */
  1364. esModuleInterop?: boolean;
  1365. /**
  1366. Allow accessing UMD globals from modules.
  1367. @default false
  1368. */
  1369. allowUmdGlobalAccess?: boolean;
  1370. /**
  1371. Resolve `keyof` to string valued property names only (no numbers or symbols).
  1372. @default false
  1373. @deprecated This option will be removed in TypeScript 5.5.
  1374. */
  1375. keyofStringsOnly?: boolean;
  1376. /**
  1377. Emit ECMAScript standard class fields.
  1378. @default false
  1379. */
  1380. useDefineForClassFields?: boolean;
  1381. /**
  1382. Generates a sourcemap for each corresponding `.d.ts` file.
  1383. @default false
  1384. */
  1385. declarationMap?: boolean;
  1386. /**
  1387. Include modules imported with `.json` extension.
  1388. @default false
  1389. */
  1390. resolveJsonModule?: boolean;
  1391. /**
  1392. Have recompiles in '--incremental' and '--watch' assume that changes within a file will only affect files directly depending on it.
  1393. @default false
  1394. */
  1395. assumeChangesOnlyAffectDirectDependencies?: boolean;
  1396. /**
  1397. Output more detailed compiler performance information after building.
  1398. @default false
  1399. */
  1400. extendedDiagnostics?: boolean;
  1401. /**
  1402. Print names of files that are part of the compilation and then stop processing.
  1403. @default false
  1404. */
  1405. listFilesOnly?: boolean;
  1406. /**
  1407. Disable preferring source files instead of declaration files when referencing composite projects.
  1408. @default true if composite, false otherwise
  1409. */
  1410. disableSourceOfProjectReferenceRedirect?: boolean;
  1411. /**
  1412. Opt a project out of multi-project reference checking when editing.
  1413. @default false
  1414. */
  1415. disableSolutionSearching?: boolean;
  1416. /**
  1417. Print names of files which TypeScript sees as a part of your project and the reason they are part of the compilation.
  1418. @default false
  1419. */
  1420. explainFiles?: boolean;
  1421. /**
  1422. Preserve unused imported values in the JavaScript output that would otherwise be removed.
  1423. @default true
  1424. @deprecated Use `verbatimModuleSyntax` instead.
  1425. */
  1426. preserveValueImports?: boolean;
  1427. /**
  1428. List of file name suffixes to search when resolving a module.
  1429. */
  1430. moduleSuffixes?: string[];
  1431. /**
  1432. Control what method is used to detect module-format JS files.
  1433. @default 'auto'
  1434. */
  1435. moduleDetection?: CompilerOptions.ModuleDetection;
  1436. /**
  1437. Allows TypeScript files to import each other with a TypeScript-specific extension like .ts, .mts, or .tsx.
  1438. @default false
  1439. */
  1440. allowImportingTsExtensions?: boolean;
  1441. /**
  1442. Forces TypeScript to consult the exports field of package.json files if it ever reads from a package in node_modules.
  1443. @default false
  1444. */
  1445. resolvePackageJsonExports?: boolean;
  1446. /**
  1447. Forces TypeScript to consult the imports field of package.json files when performing a lookup that starts with # from a file whose ancestor directory contains a package.json.
  1448. @default false
  1449. */
  1450. resolvePackageJsonImports?: boolean;
  1451. /**
  1452. Suppress errors for file formats that TypeScript does not understand.
  1453. @default false
  1454. */
  1455. allowArbitraryExtensions?: boolean;
  1456. /**
  1457. List of additional conditions that should succeed when TypeScript resolves from package.json.
  1458. */
  1459. customConditions?: string[];
  1460. /**
  1461. Anything that uses the type modifier is dropped entirely.
  1462. @default false
  1463. */
  1464. verbatimModuleSyntax?: boolean;
  1465. /**
  1466. Suppress deprecation warnings
  1467. */
  1468. ignoreDeprecations?: CompilerOptions.IgnoreDeprecations;
  1469. /**
  1470. Do not allow runtime constructs that are not part of ECMAScript.
  1471. @default false
  1472. */
  1473. erasableSyntaxOnly?: boolean;
  1474. /**
  1475. Enable lib replacement.
  1476. @default true
  1477. */
  1478. libReplacement?: boolean;
  1479. };
  1480. namespace WatchOptions {
  1481. type WatchFileKind =
  1482. | 'FixedPollingInterval'
  1483. | 'PriorityPollingInterval'
  1484. | 'DynamicPriorityPolling'
  1485. | 'FixedChunkSizePolling'
  1486. | 'UseFsEvents'
  1487. | 'UseFsEventsOnParentDirectory';
  1488. type WatchDirectoryKind =
  1489. | 'UseFsEvents'
  1490. | 'FixedPollingInterval'
  1491. | 'DynamicPriorityPolling'
  1492. | 'FixedChunkSizePolling';
  1493. type PollingWatchKind =
  1494. | 'FixedInterval'
  1495. | 'PriorityInterval'
  1496. | 'DynamicPriority'
  1497. | 'FixedChunkSize';
  1498. }
  1499. type WatchOptions = {
  1500. /**
  1501. Specify the strategy for watching individual files.
  1502. @default 'UseFsEvents'
  1503. */
  1504. watchFile?: WatchOptions.WatchFileKind | Lowercase<WatchOptions.WatchFileKind>;
  1505. /**
  1506. Specify the strategy for watching directories under systems that lack recursive file-watching functionality.
  1507. @default 'UseFsEvents'
  1508. */
  1509. watchDirectory?: WatchOptions.WatchDirectoryKind | Lowercase<WatchOptions.WatchDirectoryKind>;
  1510. /**
  1511. Specify the polling strategy to use when the system runs out of or doesn't support native file watchers.
  1512. */
  1513. fallbackPolling?: WatchOptions.PollingWatchKind | Lowercase<WatchOptions.PollingWatchKind>;
  1514. /**
  1515. Enable synchronous updates on directory watchers for platforms that don't support recursive watching natively.
  1516. */
  1517. synchronousWatchDirectory?: boolean;
  1518. /**
  1519. Specifies a list of directories to exclude from watch.
  1520. */
  1521. excludeDirectories?: string[];
  1522. /**
  1523. Specifies a list of files to exclude from watch.
  1524. */
  1525. excludeFiles?: string[];
  1526. };
  1527. /**
  1528. Auto type (.d.ts) acquisition options for this project.
  1529. */
  1530. type TypeAcquisition = {
  1531. /**
  1532. Enable auto type acquisition.
  1533. */
  1534. enable?: boolean;
  1535. /**
  1536. Specifies a list of type declarations to be included in auto type acquisition. For example, `['jquery', 'lodash']`.
  1537. */
  1538. include?: string[];
  1539. /**
  1540. Specifies a list of type declarations to be excluded from auto type acquisition. For example, `['jquery', 'lodash']`.
  1541. */
  1542. exclude?: string[];
  1543. /**
  1544. Disable infering what types should be added based on filenames in a project.
  1545. */
  1546. disableFilenameBasedTypeAcquisition?: boolean;
  1547. };
  1548. type References = {
  1549. /**
  1550. A normalized path on disk.
  1551. */
  1552. path: string;
  1553. /**
  1554. The path as the user originally wrote it.
  1555. */
  1556. originalPath?: string;
  1557. /**
  1558. True if the output of this reference should be prepended to the output of this project.
  1559. Only valid for `--outFile` compilations.
  1560. @deprecated This option will be removed in TypeScript 5.5.
  1561. */
  1562. prepend?: boolean;
  1563. /**
  1564. True if it is intended that this reference form a circularity.
  1565. */
  1566. circular?: boolean;
  1567. };
  1568. }
  1569. /**
  1570. Type for [TypeScript's `tsconfig.json` file](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html) (TypeScript 3.7).
  1571. @category File
  1572. */
  1573. type TsConfigJson = {
  1574. /**
  1575. Instructs the TypeScript compiler how to compile `.ts` files.
  1576. */
  1577. compilerOptions?: TsConfigJson.CompilerOptions;
  1578. /**
  1579. Instructs the TypeScript compiler how to watch files.
  1580. */
  1581. watchOptions?: TsConfigJson.WatchOptions;
  1582. /**
  1583. Auto type (.d.ts) acquisition options for this project.
  1584. */
  1585. typeAcquisition?: TsConfigJson.TypeAcquisition;
  1586. /**
  1587. Enable Compile-on-Save for this project.
  1588. */
  1589. compileOnSave?: boolean;
  1590. /**
  1591. Path to base configuration file to inherit from.
  1592. */
  1593. extends?: string | string[];
  1594. /**
  1595. If no `files` or `include` property is present in a `tsconfig.json`, the compiler defaults to including all files in the containing directory and subdirectories except those specified by `exclude`. When a `files` property is specified, only those files and those specified by `include` are included.
  1596. */
  1597. files?: string[];
  1598. /**
  1599. Specifies a list of files to be excluded from compilation. The `exclude` property only affects the files included via the `include` property and not the `files` property.
  1600. Glob patterns require TypeScript version 2.0 or later.
  1601. */
  1602. exclude?: string[];
  1603. /**
  1604. Specifies a list of glob patterns that match files to be included in compilation.
  1605. If no `files` or `include` property is present in a `tsconfig.json`, the compiler defaults to including all files in the containing directory and subdirectories except those specified by `exclude`.
  1606. */
  1607. include?: string[];
  1608. /**
  1609. Referenced projects.
  1610. */
  1611. references?: TsConfigJson.References[];
  1612. };
  1613. type TsConfigJsonResolved = Except<TsConfigJson, 'extends'>;
  1614. type TsConfigResult = {
  1615. /**
  1616. * The path to the tsconfig.json file
  1617. */
  1618. path: string;
  1619. /**
  1620. * The resolved tsconfig.json file
  1621. */
  1622. config: TsConfigJsonResolved;
  1623. };
  1624. type PathsMatcher = (specifier: string) => string[];
  1625. type Cache<value = any> = Map<string, value>;
  1626. /**
  1627. * Searches for a tsconfig file by walking up the directory tree.
  1628. *
  1629. * @param searchPath Path to a source file or directory to search from (default: `process.cwd()`).
  1630. * @param configName Config file name (default: `tsconfig.json`).
  1631. * @param cache Cache for previous results (default: new `Map()`).
  1632. * @param includes When true, validates that the tsconfig applies to the
  1633. * `searchPath` file via `include`/`exclude`/`files`. See {@link getTsconfig}
  1634. * for details. Default: `false`.
  1635. * @returns The path to the found tsconfig file, or `undefined` if not found.
  1636. */
  1637. declare const findTsconfig: (searchPath?: string, configName?: string, cache?: Cache, includes?: boolean) => string | undefined;
  1638. /**
  1639. * Finds and reads a tsconfig file by walking up the directory tree.
  1640. *
  1641. * By default, returns the nearest tsconfig found (matching `tsc` CLI behavior).
  1642. *
  1643. * When `includes` is true and `searchPath` is a file path, validates
  1644. * that the found tsconfig applies to the file (via `files`, `include`, and
  1645. * `exclude`). If the file isn't matched, continues searching parent directories.
  1646. * This matches VS Code's TypeScript Language Server behavior.
  1647. *
  1648. * Reference:
  1649. * - `tsc` CLI uses `findConfigFile()` which returns the nearest tsconfig without validation:
  1650. * https://github.com/microsoft/TypeScript/blob/b19a9da2a3b8/src/compiler/program.ts#L328
  1651. * - Language Server uses `isMatchedByConfig()` to verify the file belongs to the project:
  1652. * https://github.com/microsoft/TypeScript/blob/b19a9da2a3b8/src/server/editorServices.ts#L4486
  1653. *
  1654. * @param searchPath Path to a source file or directory to search from (default: `process.cwd()`).
  1655. * @param configName Config file name (default: `tsconfig.json`).
  1656. * @param cache Cache for previous results (default: new `Map()`).
  1657. * @param includes When true, validates that the tsconfig applies to the
  1658. * `searchPath` file via `include`/`exclude`/`files`. Default: `false`.
  1659. * @returns The tsconfig file path and parsed contents, or `null` if not found.
  1660. */
  1661. declare const getTsconfig: (searchPath?: string, configName?: string, cache?: Cache, includes?: boolean) => TsConfigResult | null;
  1662. /**
  1663. * Parses a tsconfig file at a given path
  1664. *
  1665. * @param tsconfigPath - Path to the tsconfig file.
  1666. * @param cache - Cache for storing parsed tsconfig results (default: new `Map()`).
  1667. * @returns The parsed and resolved tsconfig JSON.
  1668. */
  1669. declare const parseTsconfig: (tsconfigPath: string, cache?: Cache<string>) => TsConfigJsonResolved;
  1670. /**
  1671. * Reference:
  1672. * https://github.com/microsoft/TypeScript/blob/3ccbe804f850f40d228d3c875be952d94d39aa1d/src/compiler/moduleNameResolver.ts#L2465
  1673. */
  1674. declare const createPathsMatcher: (tsconfig: TsConfigResult) => PathsMatcher | null;
  1675. type FileMatcher = (filePath: string) => (TsConfigJsonResolved | undefined);
  1676. declare const createFilesMatcher: ({ config, path: tsconfigPath, }: TsConfigResult, caseSensitivePaths?: boolean) => FileMatcher;
  1677. export { type Cache, type FileMatcher, type PathsMatcher, TsConfigJson, type TsConfigJsonResolved, type TsConfigResult, createFilesMatcher, createPathsMatcher, findTsconfig, getTsconfig, parseTsconfig };