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

Asserting against type parameters (possible with dtslint) #64

Closed
leighman opened this issue Jan 15, 2020 · 4 comments
Closed

Asserting against type parameters (possible with dtslint) #64

leighman opened this issue Jan 15, 2020 · 4 comments

Comments

@leighman
Copy link

leighman commented Jan 15, 2020

We have a function

export const createTableInterface = <
  A extends string = never,
  U extends TableDetails<A> = never
>() => <K extends keyof U>(table: K) => (
  pg: PgQueryInterface,
): TableInterface<A, U, K> => {

which should be used like
createTableInterface<keyof Tables, Tables>()
where Tables is some generated type.

Slightly hacky but I think the only way to allow the user to provide the Tables interface that conforms to a particular shape.

With dtslint we could write tests like

createTableInterface<number>() // $ExpectError
createTableInterface<string, Tables>() // $ExpectError

which assert/document the behaviour of the type parameters are correct.

Tests like this don't seem to be possible with tsd. Is it something that could be supported? Are they just bad tests?

Thanks.

@sindresorhus
Copy link
Collaborator

Have you tried expectError(createTypedPgHelper<number>())?

@leighman
Copy link
Author

That fails with Type number does not satisfy the constraint string which is true but I also want to document and to assert that the constraint remains in place in the future.

@SamVerschueren
Copy link
Collaborator

You are right, it should work. We have a predefined list of errors that are catched by expectError. It's hard to know all use cases beforehand as we don't want expectError() to swallow syntactical errors for instance.

I've added the diagnostics code to the list of errors that should be swallowed by expectError. When I make a new release, it should be possible to use it like @sindresorhus suggested.

expectError(createTypedPgHelper<number>())

@SamVerschueren
Copy link
Collaborator

Released as 0.13.1

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 a pull request may close this issue.

3 participants