question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[TS] Any way to predefine the flags then typecheck them in options?

See original GitHub issue

Currently the type of flags are difficult to predefine, it’ll be easier to write the meow(..., ...) function then get the returned type.

But I’m trying to predefine the flags’ type then use it to typecheck the meow() function. e.g.:

// interfaces/config.ts

export interface IFlags {
  flag1: string;
  flag2: boolean;
  flag3: number;
}

export interface IConfig {
  argv: meow.IResult<IFlags>; // similar to meow.Result but pass in flag type not flag def
  some: string;
  other: boolean;
  configs: number[];
}
// config.ts

import {IFlags, IConfig} from './interfaces/config.ts';

const argv = anotherWayToMeow<IFlags>( // similar to current meow() but pass in flag type not def
  `Usage: xxxxxxx`,
  {
    flags: {
      // better to have typecheck here,
      // if not at least can predefine the result flags
      flag1: { alias: 'a', type: 'string' },
      flag2: { alias: 'b' }, // error because missing type: 'boolean'
      // error because missing flag3
    },
  },
)

const some = 'xxxx';
const other = process.env.MY_ENV === 'other';
const configs = [1,2,3];

const config: IConfig = { argv, some, other, configs };

export default config;

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
NiGhTTraXcommented, Dec 17, 2019

@whitetrefoil you don’t need to pass a generic type to meow if you can define your expected result type:

type Flags = {
  foo: number;
}

const cli: { flags: Flags } = meow({
  flags: {
    foo: { type: 'number' }
  }
});

You would get a type error if you tried to use type: 'string' instead because the result wouldn’t be assignable to your type.

0reactions
voxpellicommented, Jun 18, 2020

The solution suggested by @NiGhTTraX sounds like the proper one to me.

It’s even the one used in meow’s type tests:

https://github.com/sindresorhus/meow/blob/629af48c24a2f19636512b2f532cfcd9419f9be1/index.test-d.ts#L8-L16

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Advanced Types - TypeScript
This page lists some of the more advanced ways in which you can model types, it works in tandem with the Utility Types...
Read more >
20 TypeScript Flags You Should be Using | Academy
In this article, I'll show you 20 TypeScript compiler options that my team and I use and recommend to our clients. Then, we'll...
Read more >
Skip typechecking; only emit (support --transpileOnly in tsc , re ...
So, I looked for a way to do it using tsc . Turns out, it is not available, and we have to somehow...
Read more >
Overriding interface property type defined in Typescript d.ts file
In the docs for the any type it states: The any type is a powerful way to work with existing JavaScript, allowing you...
Read more >
Five tips I wish I knew when I started with Typescript - codeburst
This rule will fail linting if you explicitly define an “any” in your application and did not turn it off for the current...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found