-
-
Notifications
You must be signed in to change notification settings - Fork 104
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 Typings #48
Comments
|
actually no I'm talking about the type definitions. However it would be appropriate to publish once done to @types for ease of install with Typescript 2.x. Sorry if I wasn't clear, but yes we'd need the defs first obviously :) |
@origin1tech I'm not sure if third parties even have the rights to publish under other user's namespaces, like the I personally have never written typings before, so probably someone who knew how to write them would have to (I think @blakeembrey does). |
@origin1tech I do know what you're talking about. DefinitelyTyped is the location you should ask if you want something published to |
@dougwilson We can't, and we wouldn't want to. Once someone has written a definition, we can add it to the module ourselves and publish to NPM directly (using |
@blakeembrey I'm fully aware of that....my point is simply that it would be nice to get defs for router and then publish an @types project for ease of install. I get that they are unrelated, however if the author creates the definitions it's helpful as there's less chance of things getting missed. |
@origin1tech You're missing the point. Please re-read my comments. If the goal is "ease of install" we would add them to this module, and not @types. That way, as a TypeScript user, you don't have to do anything to have it work - which is ease of install to me, compared to having to install an additional module. That's why I suggested keeping it open for that, and to ignore @types. |
Maybe I'm missing something but Typescript is suggesting @types as the path for installing defs. I talk to some of those fellas on the regular pretty certain on that. see https://blogs.msdn.microsoft.com/typescript/2016/06/15/the-future-of-declaration-files/ |
@origin1tech that article has the following line:
And you are talking to Blake in this very thread :) |
@origin1tech You're incorrect. That approach is relevant only for third-party typings (E.g. those from Typings/DefinitelyTyped). Modules themselves can publish their own type definitions, and that has been the preferred approach for first-party typings since TypeScript 1.5. Since you opened the issue on the module itself, I would assume the best approach is the easiest for you as a user. |
@blakeembrey how does that work if you exclude "node_modules" in your tsconfig.json. How does it get imported unless you specify it in "typeRoots" manually? |
@origin1tech You can read up on how
The process of looking up native typings is related to |
If you've used Typings before, |
@blakeembrey seen that before but was under the impression any external module, best practice would be @types. testing it on some of own projects really quick... |
But why? Imagine you have a native TypeScript project. It wouldn't make any sense to make the author's life more complicated. Just like here, if the author's are willing to have a |
@blakeembrey No I'm agreeing on that one...makes me wonder why bother with a separate registry at all. seems like best practice would be for someone to create defs then make a PR. I can't imagine many authors would be too opposed to that. |
There are some. It's a maintenance hassle for authors that don't care about TypeScript support natively. Also, sometimes the author just doesn't have someone good at TypeScript. For Typings, I tried to alleviate this by offering support everywhere (see typings/typings#322) but it's still tricky because GitHub doesn't have org mentions. For example, recently I wanted to use Facebook's Edit: Not to conflate those two issues, but having the third-party location does make sense. It can also be helpful for "beta"/incomplete typings. For example, not all typings are finished right away - many can require many iterations to fix. Having to make many PRs to something else can increase friction. I can only imagine what'd it be like for me developing https://github.com/types/npm-rethinkdb but under @rethinkdb without proper access myself. There's times where I make a dozen changes because it's a huge definition. |
Right...yeah run into that a fair amount. Getting better but early on. Well in big projects it's worth it in our opinion. We you have so many working on a proj it saves a lot of headache just in refactoring alone. But ur right lots of odd defs with things exported multiple times missing overloads on methods lots of stuff see it all at this point. ...see quite a bit of this with Ionic not too long ago just as an example. TS is a bit of an afterthought I think for them |
Any update on this issue? Thanks! |
@franciscosucre You're certainly welcome to submit an initial PR and we can go from there 😄 |
Hello @blakeembrey i am no typing expert. I would not know how to start this typings. For anybody still looking for a solution, this allowed me to continue to work with typescript using the router. Microsoft has a type definition generator, it is not perfect but it allowed to me to continue to work. npm install -g dts-gen
dts-gen.cmd -m router I do not know if the generated typings could work out for the devs as a starting point @blakeembrey . |
¿How would i upload this type definitions? It is a single file and i would not know where to use place them in this proyect ¿ Can i upload them here in this issue or something? |
I've been looking into this, and I'm keen to get typings for this package. This is the most popular package I've ever seen without any typings! The issue I see currently is: function Router(options) {
function router(req, res, next) {
router.handle(req, res, next)
}
// inherit from the correct prototype
setPrototypeOf(router, this)
return router
}
Router.prototype = function () {} The docs then say to execute route matching using: var router = Router()
var server = http.createServer(function onRequest(req, res) {
router(req, res, finalhandler(req, res))
}) This overriding the prototype constructor doesn't appear possible with typescript definitions (although if someone knows I'd love to know how). It also seems weird and deviates from If people are willing to drop this returned method and make a breaking change to a new version. This would be dropping the constructor prototype override and instead moving to the |
I'm not sure what this is, can you elaborate?
I'm not really interested in making such a breaking change if the only purpose is to support typescript. This module is written in javascript to be javascript and use the features that javascript makes available. If the desire was to restrict the code to only what is possible in typescript, the code would just have been written in typescript. Someone can always make a typescript friendly fork I assume, no? |
I am not sure why this would be necessary. Is the issues that TS typing cannot understand inheriting from a function? If so, then seems like an issue on their end. If I understand the issue correctly, could it not just pretend it is an object? I know it would ignore it's 'function'-ness in the documentation/autocomplete, but is that really an issue? |
One additional thought I had to add to the above: is it actually impossible to represent in typescript definitions, or are you just saying you're not sure how to represent it in the definitions? |
Apologies. I think it is possible. I've hacked together some of the api using the declare module 'router' {
interface RequestHandler {
(req: Request, res: Response, next: NextFunction): any;
}
interface IRouter {
get: IRouterMatcher<this>;
post: IRouterMatcher<this>;
put: IRouterMatcher<this>;
delete: IRouterMatcher<this>;
patch: IRouterMatcher<this>;
options: IRouterMatcher<this>;
head: IRouterMatcher<this>;
use: (path: PathParams, router: IRouter) => void;
handle: RequestHandler
}
interface RouterConstructor extends IRouter {
new(): IRouter & RequestHandler;
}
var Router: RouterConstructor;
export default Router;
} This would be without a breaking change. Edit: const router = new Router()
router.get('/whatever', (request: Request, response: Response) => undefined);
router(request, response, finalErrorHandler); TODO: handle |
// router.d.ts
// Type definitions for router
declare module 'router' {
import { NextFunction, NextHandleFunction } from 'connect'
import { IncomingMessage, ServerResponse } from 'http'
export type Path = string | RegExp | Array<string | RegExp>
export namespace Router {
export interface RouteType {
new (path: string): Route
prototype: Route
}
type Method = 'all' | 'head' | 'get' | 'post' | 'delete' | 'put' | 'patch' | 'options'
export type Route = { readonly path: Path } & Record<Method, (middleware: NextHandleFunction, ...middlewares: NextHandleFunction[]) => Route>
export interface Options {
caseSensitive?: boolean
strict?: boolean
mergeParams?: <C extends {}, P extends {}>(currentParams: C, parentParams: P) => Record<string | number, any>
}
export type ParamCallback<K = string | number> = (
req: IncomingMessage,
res: ServerResponse,
next: NextFunction,
value: any,
name: K,
) => any
interface InnerRouter extends NextHandleFunction {
route(path: Path): Route
param: <K extends string | number>(name: K, fn: ParamCallback<K>) => this
}
export type Router = InnerRouter & Record<'use' | Method, {
(path: Path, middleware: NextHandleFunction, ...middlewares: NextHandleFunction[]): Router
(middleware: NextHandleFunction, ...middlewares: NextHandleFunction[]): Router
}>
interface RouterType {
new (options?: Options): Router
(options?: Options): Router
Route: RouteType
prototype: Router
}
}
export type RouterType = Router.RouterType
const Router: RouterType
export default Router
} |
any update on this? :((( |
Any chance of getting some @types/router typings published? Or are there any floating around that we can make official and publish.
The text was updated successfully, but these errors were encountered: