-
-
Notifications
You must be signed in to change notification settings - Fork 848
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
TypeScript fixes #217
TypeScript fixes #217
Conversation
@calebboyd Does this PR address #215 (comment)? |
Edit: Yes. It would solve the need to find and replace the import method. (it adds named exports) But as for the example: function logBold(msg): string {
return chalk.bold(msg)
}
Line 19 in d86db88
AFAIK the only supported behavior is accepting string rest args. |
@calebboyd is there a way to specify |
Since its rest args it has to be an array type. eg. the following will work. chalk.bold('a')
chalk.bold('a','b','c') Is there something missing that isn't captured by that? |
@Qix- I was able to modify the overload order to provide better implicit results. Also fixed:
Sorry for all the churn here... There is more to the API than is immediately apparent.... |
I've changed my mind (Sorry @calebboyd ...). I don't think we should do the named exports. I thought it could be a nice transition step, but it's a day later now and it doesn't seem to be a big problem, and the few that had a problem have fixed it and moved on, so I think it's better for everyone that we just stick to a default export than inconvenience everyone with two ways of using Chalk, one of which is IMHO a bad practice, as I've elaborated on in #215 (comment). |
Okay Cool. (I totally agree) I'll Clean it up. The other fixes are still valid I think |
types/index.d.ts
Outdated
(...text: string[]): string; | ||
(text: TemplateStringsArray, ...placeholders: string[]): string; | ||
constructor: Chalk; | ||
new (options?: ChalkOptions): Chalk; | ||
(options: ChalkOptions): Chalk; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are these added here? They're only valid for chalk.constructor()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I missed moving them when I pulled ChalkConstructor out 😬
@@ -86,5 +92,6 @@ export interface Chalk { | |||
bgWhiteBright: Chalk; | |||
} | |||
|
|||
declare function chalk (): any; | |||
export default chalk as Chalk; | |||
declare const chalk: Chalk & { supportsColor: ColorSupport }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was & { supportsColor: ColorSupport }
moved out of the Chalk
interface? Or rather, what's the benefit of having it here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAICT supportsColor
only exists on the default export, correct? If it is left in the Chalk
interface it will show up all over where it doesn't exist.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean that this would be valid as well?
chalk.red.supportsColor
I think you're right, it shouldn't.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good point and I agree, but shouldn't that apply to constructor
, enabled
, and level
too then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, but that's not what it looks like at runtime.
These things are possible:
chalk.blue.enabled
chalk.blue.constructor
chalk.blue.level
So if it shouldn't be possible, we will have to rewrite some internals so that the type definitions match what we have at runtime.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean that this would be valid as well?
Yeah
or it could be changed to behave like enabled
and level
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, there might be some use-case where people want to do:
export const color = chalk.blue.bold;
import {color} from '.';
color.enabled = false;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sindresorhus, good use-case -- and that is supported with these typings. But color.supportsColor
would not be (nor does it exist at runtime).
should supportsColor
be added to the chained/built chalk properties?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only if we add it in the runtime, which is a separate discussion to this PR. I'm undecided whether it makes sense though.
types/index.d.ts
Outdated
@@ -13,20 +13,26 @@ export interface ChalkOptions { | |||
level?: Level; | |||
} | |||
|
|||
export interface Chalk { | |||
export interface ChalkConstructor extends Chalk { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't look right, Is chalk
a class or an object callable with new
?
types/index.d.ts
Outdated
hasBasic: boolean; | ||
has256: boolean; | ||
has16m: boolean; | ||
}; | ||
rgb(r: number, g: number, b: number): Chalk; | ||
hsl(h: number, s: number, l: number): Chalk; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of defining that all these methods return Chalk
, you can also say that they return this
.
rgb(r: number, g: number, b: number): this;
That's also what TS generates for the type definitions. Just another approach though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated
Chiming in here: Each "color" is an object with a getter: Lines 55 to 64 in f653b06
Setting these values results in a run-time error: So these typings should change from: reset: this;
bold: this;
dim: this;
... to: readonly reset: this;
readonly bold: this;
readonly dim: this;
... |
@dwieeb You're right! |
Thank you so much for helping out with this @calebboyd :) |
#217 (comment)