meow icon indicating copy to clipboard operation
meow copied to clipboard

Expected behavior is unclear when using types and non-camelcase

Open kirbysayshi opened this issue 5 years ago • 5 comments
trafficstars

Hello!

I came across a gotcha and wanted to document it for future users of this library.

As demonstrated by this sandbox https://codesandbox.io/s/meow-kebab-case-behavior-9rpoc, the type hints are inconsistent with the actual shape of the parsed object. The code below is from the sandbox to demonstrate:

import meow from "meow";

const cli = meow("", {
  flags: {
    "input-file": {
      type: "string",
      alias: "i",
      default: "INPUT"
    }
  }
});

const camel = cli.flags.inputFile;
const kebab = cli.flags["input-file"];

type C = typeof camel; // unknown
type K = typeof kebab; // string;

console.log(camel, kebab);
// yet this outputs:
// > INPUT undefined

This leads to some tricky behavior when using meow, TS, and normalized names. Specifically, type hints and autocompletion will show cli.flags['input-file'] as the type-safe suggestion, when it's actually undefined.

I don't think it's possible for meow to handle this due to how TS generics work. But hopefully this saves someone else time that I spent trying to figure this out :D Unfortunately I don't know of a type-safe workaround.

And there's always the chance I'm doing something wrong, and if so, sorry for the noise. Thanks for the library!

kirbysayshi avatar Mar 20 '20 03:03 kirbysayshi

// @NiGhTTraX Is there anything we can do to improve this other than better docs about it?

sindresorhus avatar Mar 20 '20 04:03 sindresorhus

Unfortunately, no. We can't express the normalization of the flags through the type system. However, unnormalizedFlags should contain the flag under its original name with the correct type so in your case cli.unnormalizedFlags['input-file'] // number. It would be nice if this behavior were documented.

NiGhTTraX avatar Mar 20 '20 08:03 NiGhTTraX

I tell you h'wat - you can: https://github.com/microsoft/TypeScript/pull/40336

Robbie-Cook avatar Nov 25 '20 03:11 Robbie-Cook

Note the Split<>

Robbie-Cook avatar Nov 25 '20 03:11 Robbie-Cook

We can now use https://github.com/sindresorhus/type-fest/blob/master/ts41/camel-case.d.ts PR welcome :)

sindresorhus avatar Nov 25 '20 12:11 sindresorhus

Fixed by https://github.com/sindresorhus/meow/issues/155

sindresorhus avatar Oct 11 '22 03:10 sindresorhus