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

fix: TypeScript declaration - strict label values #299

Merged
merged 3 commits into from
Nov 7, 2019

Conversation

kobiburnley
Copy link
Contributor

No description provided.

@SimenB
Copy link
Collaborator

SimenB commented Nov 5, 2019

Hey! Could you explain what this changes in usage?

@kobiburnley
Copy link
Contributor Author

@SimenB hello!

This gives TypeScript users stricter type checking on labels (verifies label keys against the labels array provided in the metric constructor)

type LabelValues<T extends string> = Partial<Record<T, string | number>>;

class Counter<T extends string> {
    constructor(public labelNames: T[]) {

    }

    inc(LabelValues: LabelValues<T>) {

    }
}

const counter = new Counter(["method", "statusCode"])

counter.inc({
    method: "",
    statusCode: "sdgsgd"
})

counter.inc({
    method: "",
    statuesCode: "sdgsgd" // won't compile :)
})

demo

@SimenB
Copy link
Collaborator

SimenB commented Nov 6, 2019

Very nice, thank you! Could you update the changelog as well?

@kobiburnley
Copy link
Contributor Author

@SimenB done.

@ankon
Copy link
Contributor

ankon commented Feb 20, 2020

This change now requires quite some changes in user code, from

interface Metrics {
	LAST_PING_RECEIVED_ID: prometheus.Gauge;
}

to

interface Metrics {
	LAST_PING_RECEIVED_ID: prometheus.Gauge<string>;
}

I'm not sure why the type T is required there, would someone be using something else than a string here? Could it be defaulted to T extends string = string?

@sam-github
Copy link
Contributor

This makes me wonder if some tests actually written in typescript against the ts defs would be useful, to confirm that the ts defs are actually valid and useful (speaking as a non-ts developer who has had to update the defs lately, and did so with trepidation).

@siimon
Copy link
Owner

siimon commented Feb 21, 2020

Yeah you are probably right.. I think the only test right now just sees if the typefile compiles, not that it really works as expected

@ankon
Copy link
Contributor

ankon commented Feb 21, 2020

This makes me wonder if some tests actually written in typescript against the ts defs would be useful, to confirm that the ts defs are actually valid and useful (speaking as a non-ts developer who has had to update the defs lately, and did so with trepidation).

As a first step: Instead of writing "tests" you could write an example, and check that the compiler is happy about it, i.e. can compile the example code against the type descriptions. When converting to typescript we even started one step earlier and simply let tsc compile the definitions as a check, to kill out the odd syntax issues and make developers more comfortable with the .d.ts files.

But, that's probably a different issue -- for the API change related to the now-required type parameters, do you think I should whip together a PR and try to "undo" this?

@SimenB
Copy link
Collaborator

SimenB commented Feb 21, 2020

https://www.npmjs.com/package/tsd is pretty nice for writing type tests.

But yeah, defaulting to string should work fine, I think?

@ankon
Copy link
Contributor

ankon commented Feb 21, 2020

I think so too, yes.

@zbjornson
Copy link
Collaborator

zbjornson commented Feb 21, 2020

I'd go for Microsoft's dtslint for testing and linting declarations, https://github.com/microsoft/dtslint. Easy to use, handles different versions of tsc well, and I think it's what all of DefinitelyTyped uses.

@dt-rush
Copy link

dt-rush commented Apr 19, 2020

This might be a nice feature to be able to enable should the developer choose to enable it. As discussed in #298 there's actually no reason to require pre-declaration of labelsets and the more that we double-down on this necessity the less flexible the client library is for a whole variety of use cases where labelsets are not known in advance.

Unfortunately, it's added in such a way that it's not optional, this type information is a permanent part of what the typescript compiler desires. Therefore in line with a PR to solve #298 I'll be including a reversion of this (a reversion was also desired for an additional valid reason by @ankon above).

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.

7 participants