unjs / citty

🌆 Elegant CLI Builder
Other
729 stars 23 forks source link

Type error on arguments #148

Closed Teages closed 3 months ago

Teages commented 3 months ago

Environment

Reproduction

just use ~/playground/hello.ts

Here is a copy:

import consola from "consola";
import { defineCommand, createMain } from "../src";

const command = defineCommand({
  meta: {
    name: "hello",
    version: "1.0.0",
    description: "My Awesome CLI App",
  },
  args: {
    name: {
      type: "positional",
      description: "Your name",
      required: true,
    },
    friendly: {
      type: "boolean",
      description: "Use friendly greeting",
    },
    age: {
      type: "number",
      description: "Your age",
      required: false,
    },
    adj: {
      type: "enum",
      description: "Adjective to use in greeting",
      options: ["awesome", "cool", "nice"],
      default: "awesome",
      required: false,
    },
  },
  run({ args }) {
    consola.log(args);
    const msg = [
      args.friendly ? "Hi" : "Greetings",
      args.adj || "",
      args.name,
      args.age ? `You are ${args.age} years old.` : "",
    ]
      .filter(Boolean)
      .join(" ");

    consola.log(msg);
  },
});

createMain(command)({});

Describe the bug

There are two errors on type:

  1. args.adj should be string or string | number but now it is string[], in further it had better to be 'awesome' | 'cool' | 'nice'
  2. friendly could be undefined if not provided, but here typescript think it is non-nullable

(although age is not required, it will throw an error Invalid value for argument: --age (undefined). Expected a number. if not provided age. It is not related to this issue)

Additional context

I will have a pr to fix the problem.

Logs

No response

Teages commented 3 months ago

will resolve by #132