Skip to content

Commit

Permalink
feat(experimental-utils): allow rule options to be a readonly tuple (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ulrichb committed Apr 21, 2020
1 parent 017b22d commit 4ef6788
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
Expand Up @@ -17,7 +17,7 @@ function RuleCreator(urlCreator: (ruleName: string) => string) {
// This function will get much easier to call when this is merged https://github.com/Microsoft/TypeScript/pull/26349
// TODO - when the above PR lands; add type checking for the context.report `data` property
return function createRule<
TOptions extends unknown[],
TOptions extends readonly unknown[],
TMessageIds extends string,
TRuleListener extends RuleListener = RuleListener
>({
Expand Down
8 changes: 6 additions & 2 deletions packages/experimental-utils/src/eslint-utils/applyDefault.ts
Expand Up @@ -7,12 +7,14 @@ import { deepMerge, isObjectNotArray } from './deepMerge';
* @param userOptions the user opts
* @returns the options with defaults
*/
function applyDefault<TUser extends unknown[], TDefault extends TUser>(
function applyDefault<TUser extends readonly unknown[], TDefault extends TUser>(
defaultOptions: TDefault,
userOptions: TUser | null,
): TDefault {
// clone defaults
const options: TDefault = JSON.parse(JSON.stringify(defaultOptions));
const options: AsMutable<TDefault> = JSON.parse(
JSON.stringify(defaultOptions),
);

if (userOptions === null || userOptions === undefined) {
return options;
Expand All @@ -33,4 +35,6 @@ function applyDefault<TUser extends unknown[], TDefault extends TUser>(
return options;
}

type AsMutable<T extends {}> = { -readonly [TKey in keyof T]: T[TKey] };

export { applyDefault };

0 comments on commit 4ef6788

Please sign in to comment.