Description
I was very cautious to write libraries in Typescript, I was always thinking that it might increase barriers to contributors. But we won’t get around having typescript definitions and the constant problems that occur due to JavaScript code being out-of-sync with the Typescript definitions I think it’s time to embrace Typescript.
If you have any concerns, now is the time to raise them. I’ll probably rewrite all @octokit/*
libraries to Typescript.
I started experimenting with Typescript definitions for @ocotkit/endpoint
a while ago:
type KnownRoute =
| 'GET /foo'
| 'POST /foo'
type Method =
| 'DELETE'
| 'GET'
| 'HEAD'
| 'PATCH'
| 'POST'
| 'PUT'
type KnownPath =
| '/foo'
type Options = {
headers?: Object,
request?: Object,
baseUrl?: string,
data?: any
[option: string]: any
}
type KnownRouteOptions = {
method: Method,
url: KnownPath
}
type CustomRouteOptions = {
method: Method,
url: string
}
type RequestOptions = {
method: Method,
url: string,
headers: object,
body?: any,
request?: object
}
type GetFooRoute = 'GET /foo'
type GetFooOptions = {
headers?: object,
request?: object,
bar: string,
baz:
| 'one'
| 'two'
| 'three'
}
type GetFooRequestOptions = {
method: 'GET',
url: string,
headers: object,
request?: object
}
/**
* Super funky fresh!
*
* @param route funky
* @param options fresh
*/
function endpoint (route: GetFooRoute, options: GetFooOptions): GetFooRequestOptions;
function endpoint (route: KnownRoute, options?: Options): RequestOptions;
function endpoint (route: string, options?: Options): RequestOptions;
function endpoint (options: Options & KnownRouteOptions): RequestOptions;
function endpoint (options: Options & CustomRouteOptions): RequestOptions;
function endpoint (route?, options?): RequestOptions {
return {
method: 'GET',
url: '/foo',
headers: {}
}
}
My thinking is that we could use @octokit/routes
to generate typescript definitions for every known route, I think that would provide a very sleek developer experience.
The same definitions can be inherited by @octokit/request
, so people might end up just using this library in order to minimize their bundle size, but still have nice typeahead experience.