types.ts icon indicating copy to clipboard operation
types.ts copied to clipboard

🚧 [Help Wanted] types for `endpoint(options)`

Open gr2m opened this issue 5 years ago • 1 comments
trafficstars

This pull requests attempts to introduce type validations for endpoint(options). It looks for the options.method and options.url and compares them to known routes. If there is a match, it enforces the required options for that route and returns a better typed result.

The problem is that TypeScript throws an error for EndpointsByUrlAndMethod[O["url"]][O["method"]]["request"]

Expression produces a union type that is too complex to represent

I don't know if that is resolvable at all. I might have hit a limit of what TypeScript is capable of doing.

The reason I'd like to make this possible is that we would no longer require defining custom type definitions for rest endpoint methods as we do in https://raw.githubusercontent.com/octokit/plugin-rest-endpoint-methods.js/53e3403d39e35b25ac3b5ae872895ab4277067cd/src/generated/types.ts, because all typings would be set correctly by doing sth like this

import { Octokit } from `@octokit/core`
const octokit = new Octokit({ auth: 'secret123' })
const createIssue = request.defaults({ method: 'POST', url: '/repos/:owner/:repo/issues' })

If anyone is up for a challenge, it's all yours :)

gr2m avatar Apr 19 '20 17:04 gr2m

I worked around the error in cf2782a, but some functionality is missing (see test.ts). Plus, probably more importantly, the TypeScript Intellisense in VS Code got unusable slow

gr2m avatar Apr 19 '20 18:04 gr2m