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

TypeScript fixes #217

Merged
merged 6 commits into from
Oct 24, 2017
Merged

TypeScript fixes #217

merged 6 commits into from
Oct 24, 2017

Conversation

calebboyd
Copy link
Contributor

@calebboyd calebboyd commented Oct 18, 2017

@Qix-
Copy link
Member

Qix- commented Oct 18, 2017

@calebboyd Does this PR address #215 (comment)?

@calebboyd
Copy link
Contributor Author

calebboyd commented Oct 18, 2017

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)
}

logBold accepts an implicit any, which isn't supported by any signature that we have defined...
If we wanted to support that we could add the type:
(...text: any[]): string here:

(...text: string[]): string;

AFAIK the only supported behavior is accepting string rest args.

@Qix-
Copy link
Member

Qix- commented Oct 18, 2017

@calebboyd is there a way to specify string | ...string[]?

@calebboyd
Copy link
Contributor Author

calebboyd commented Oct 18, 2017

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?

@calebboyd
Copy link
Contributor Author

calebboyd commented Oct 19, 2017

@Qix- I was able to modify the overload order to provide better implicit results.

Also fixed:

  • an options object is required to return a chalk instance instead of a string
  • supportsColor is now correctly only defined as a property of the default export and is no longer circular
  • constructor has its own type to support no options being passed

Sorry for all the churn here... There is more to the API than is immediately apparent....

@calebboyd calebboyd changed the title Named exports TypeScript named exports Oct 19, 2017
@sindresorhus
Copy link
Member

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).

@calebboyd
Copy link
Contributor Author

Okay Cool. (I totally agree) I'll Clean it up. The other fixes are still valid I think

@calebboyd calebboyd changed the title TypeScript named exports Typescript fixes Oct 19, 2017
@pelotom pelotom mentioned this pull request Oct 21, 2017
types/index.d.ts Outdated
(...text: string[]): string;
(text: TemplateStringsArray, ...placeholders: string[]): string;
constructor: Chalk;
new (options?: ChalkOptions): Chalk;
(options: ChalkOptions): Chalk;
Copy link
Member

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()

Copy link
Contributor Author

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 };
Copy link
Member

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?

Copy link
Contributor Author

@calebboyd calebboyd Oct 22, 2017

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.

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.

Copy link
Member

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?

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.

Copy link
Contributor Author

@calebboyd calebboyd Oct 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SamVerschueren

You mean that this would be valid as well?

Yeah

or it could be changed to behave like enabled and level

Copy link
Member

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;

Copy link
Contributor Author

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?

Copy link
Member

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.

@sindresorhus sindresorhus changed the title Typescript fixes TypeScript fixes Oct 21, 2017
types/index.d.ts Outdated
@@ -13,20 +13,26 @@ export interface ChalkOptions {
level?: Level;
}

export interface Chalk {
export interface ChalkConstructor extends Chalk {

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;

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

@imhoffd
Copy link

imhoffd commented Oct 23, 2017

Chiming in here:

Each "color" is an object with a getter:

chalk/index.js

Lines 55 to 64 in f653b06

for (const key of Object.keys(ansiStyles)) {
ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g');
styles[key] = {
get() {
const codes = ansiStyles[key];
return build.call(this, this._styles ? this._styles.concat(codes) : [codes], key);
}
};
}

Setting these values results in a run-time error: TypeError: Cannot set property green of #<Chalk> which has only a getter.

So these typings should change from:

	reset: this;
	bold: this;
	dim: this;
...

to:

	readonly reset: this;
	readonly bold: this;
	readonly dim: this;
...

@calebboyd
Copy link
Contributor Author

@dwieeb You're right!

@sindresorhus sindresorhus merged commit 7be154c into chalk:master Oct 24, 2017
@sindresorhus
Copy link
Member

Thank you so much for helping out with this @calebboyd :)

@calebboyd calebboyd deleted the named-exports branch October 24, 2017 13:12
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

Successfully merging this pull request may close these issues.

None yet

6 participants