[TS] Any way to predefine the flags then typecheck them in options?
See original GitHub issueCurrently 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:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@whitetrefoil you don’t need to pass a generic type to
meow
if you can define your expected result type:You would get a type error if you tried to use
type: 'string'
instead because the result wouldn’t be assignable to your type.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