Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect types for chalk in tagged template literal form #314

Closed
vanbujm opened this issue Dec 20, 2018 · 9 comments
Closed

Incorrect types for chalk in tagged template literal form #314

vanbujm opened this issue Dec 20, 2018 · 9 comments
Milestone

Comments

@vanbujm
Copy link
Contributor

vanbujm commented Dec 20, 2018

I am using chalk to do some logging and I noticed this issue.

chalk`{black.bold  Worker:${process.pid}: } {blue.bold job received}: ${job.id}`

In this case I am giving chalk two numbers instead of strings.
Typescript complains about this because chalk's tagged template literal form is defined as

(text: TemplateStringsArray, ...placeholders: string[]): string;

In my case placeholders contains numbers.

Now I could fix this by doing

chalk`{black.bold  Worker:${`${process.pid}`}: } {blue.bold job received}: ${`${job.id}`}`

but that feels wrong.

Ideally it would be some type union of things easily parsed to string (||etc etc)

(text: TemplateStringsArray, ...placeholders: ParseableToString[]): string;
@Qix-
Copy link
Member

Qix- commented Dec 20, 2018

Yeah agreed, this is wrong. Can someone from the typescript community submit a fix? I don't know enough of it.

@vanbujm
Copy link
Contributor Author

vanbujm commented Dec 21, 2018

I am happy to submit a PR if someone can define the type that it should be. A quick fix would be to change it to any[] but it is probably better to define some type union.

@Qix-
Copy link
Member

Qix- commented Dec 21, 2018

You can interpolate any type. We don't enforce anything.

@vanbujm
Copy link
Contributor Author

vanbujm commented Dec 21, 2018

The typing enforces that the values you interpolate are strings.
index.d.ts:30 as seen above:
(text: TemplateStringsArray, ...placeholders: string[]): string;

Unless I am mistaken this line defines the tagged template literal.
where text will be your broken up input string around each ${} and placeholders is an array of inputs that the user is trying to interpolate.

ie:

chalk`part1 ${foo} part2`

Would be chalk( ["part1 ", " part2"], [foo] )
If foo was anything other than a string, Typescript will complain saying "Argument is not assignable to type string".

@Qix-
Copy link
Member

Qix- commented Dec 21, 2018

And what I'm saying is we don't care what the placeholder types are, much less that they are strings...

@vanbujm
Copy link
Contributor Author

vanbujm commented Dec 21, 2018

Shall I submit a fix then with type any?

@Qix-
Copy link
Member

Qix- commented Dec 21, 2018

Sure, but I'd like to get a confirmation that any is the correct thing to do here. Like I said, I have no idea how Typescript types work.

@sindresorhus
Copy link
Member

Yes, it should be any or rather unknown would be even better.

@langpavel
Copy link

langpavel commented Dec 28, 2018

Ohh, sorry, reference from 317 is invalid (should be to 318 not to this 314). Fixed, but reference will stay :-\

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants