Skip to content

TypeScript typings error for handlers #62

Closed
@jakehamilton

Description

@jakehamilton

The current types in index.d.ts produce an error when attempting to use them in a purely TypeScript project:

Exported variable 'args' has or is using name 'flagSymbol' from external module
"/path/to/project/node_modules/arg/index" but cannot be named.ts(4023)

After a brief bit of tweaking, I found that this can be fixed by moving flagSymbol into the arg namespace and exporting it along with other members. This makes the type accessible and resolves the error. I'm unsure if there is a better way to handle it, though this should be fine.

My Local arg.d.ts file
declare module "arg" {
	function arg<T extends arg.Spec>(
		spec: T,
		options?: arg.Options
	): arg.Result<T>;

	namespace arg {
		export const flagSymbol: unique symbol;

		export function flag<T>(fn: T): T & { [arg.flagSymbol]: true };

		export const COUNT: Handler<number> & { [arg.flagSymbol]: true };

		export type Handler<T = any> = (
			value: string,
			name: string,
			previousValue?: T
		) => T;

		export class ArgError extends Error {
			constructor(message: string, code: string);

			code: string;
		}

		export interface Spec {
			[key: string]: string | Handler | [Handler];
		}

		export type Result<T extends Spec> = { _: string[] } & {
			[K in keyof T]?: T[K] extends Handler
				? ReturnType<T[K]>
				: T[K] extends [Handler]
				? Array<ReturnType<T[K][0]>>
				: never;
		};

		export interface Options {
			argv?: string[];
			permissive?: boolean;
			stopAtPositional?: boolean;
		}
	}

	export = arg;
}

Note: This is occurring for me using typescript@^4.6.3. I'm not sure if this is an issue with a new version of TypeScript or has always been a problem since I've typically only used arg in JavaScript projects up until now.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions