sindresorhus / meow

🐈 CLI app helper
MIT License
3.53k stars 150 forks source link

Flags are not working with typescript ^4.1.3 #172

Open jpgorman opened 3 years ago

jpgorman commented 3 years ago

os: windows10 node: v10.15.1 terminal: WSL

const options = {
  flags: {
    rainbow: {
     type: 'boolean',
     alias: 'r'
    }
  }
}

meow(`...`, options)

image

sindresorhus commented 3 years ago

Try:

const options = {
  flags: {
    rainbow: {
     type: 'boolean',
     alias: 'r'
    }
  }
} as const;

meow(`...`, options)

or putting the object literal inline.

jpgorman commented 3 years ago

Thanks for the very fast response.

I've taken a look and I can confirm that:

The first option of using const results in a syntax error.

The second option of passing the options inline does remove that type error, however I'm not getting a typed interface on the return meow object.

The problem with the second option though is that you can't then use the options outside of that function call.

jpgorman commented 3 years ago

Would it be an option to export the options type and then use that when defining my options object e.g,


import { Options } from 'meow'

const options: Options = {
  flags: {
    rainbow: {
     type: 'boolean',
     alias: 'r'
    }
  }
sindresorhus commented 3 years ago

The first option of using const results in a syntax error.

Make sure you're on the latest TS version.

jpgorman commented 3 years ago

I'm currently using 4.1.3

sindresorhus commented 3 years ago

This passes: https://github.com/sindresorhus/meow/commit/a0ce744290cb72e06fb9c4af70b7fbda0327d949

jpgorman commented 3 years ago

What version of typescript are you using to test types? I wasn't sure after looking at the package.json

jpgorman commented 3 years ago

Also I check that it isn't a case of my IDE not respecting const assertions

jpgorman commented 3 years ago

So I tried again using the const assertions approach and I was getting the same type errors.

sindresorhus commented 3 years ago

TS 4.1.2

jpgorman commented 3 years ago

Thanks for your help. I'm not sure why i'm getting such a different result. For now I've stubbed out the types for the library to get it working in my project.

Caryyon commented 3 years ago

Getting something similar using npx create-ink-app

Screen Shot 2021-05-05 at 8 22 53 PM
LitoMore commented 3 years ago

The root cause is create-ink-app always using the latest version of meow and @sindresorhus/tsconfig. create-ink-app is using a simple install script to install them without a specific version. By default, you will get the latest version.

Since meow@10.0.0, we moved our package to ESM.

I will help create-ink-app move their package to ESM. It will take some time because there are some blocking dependencies that need to be moved to ESM as well.

Before that, you could downgrade your meow to 9.0.0 or lower manually (just modify your generated code and reinstall dependencies).

Update:\ I submitted a quick fix for CommonJS modules.