-
-
Notifications
You must be signed in to change notification settings - Fork 376
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 Support [Help Wanted] #589
Comments
Any reason for moving type definitions outside every project and into I personally prefer to keep them colocated just to make it easier to update them every time something changes. Am I missing something important here? |
It was more of a question on what is TS best practice. See #591 |
Best practice would be to have the type annotations right next to the code. If they are a separate file anyway, then maintaining them inside the repo in my experience works a bit better, as long as there are people who actively keep them updated and whose PRs are merged on time. |
Thanks for commenting and bring attention to that last discussion. I've read a couple times today, will likely need another read. I'm wondering how others like Happy to accept any help you can spare. I've updated the list at the top highlight deficiencies in v2. Just open a PR, I'm pretty active these days. |
I'll take a look. I fear that there might be further complications, though, as TypeScript will need to know the order in which middlewares are applied, and this order only partially depends on the order in which the middleware is added. |
There are some lesser known hooks in core that might help.
|
Not to fuss much, but we are initially in the process of migrating to TS and we use typescript compiler to generate types from JS Doc and it seems to be working for some of our internal packages, maybe the core maintainers could give that a look, until someone from the community takes charge of it. |
@KillDozerX2 I tried to do that for v2, but was missing the ability to load in external custom definitions (if I remember correctly). In the end we ended up with what we have now, I far as I know it's serving peoples needs for now. |
Hey there ;) I use middy just for the If we look at the definition of I'm thinking of creating a new PR to remove all |
@J4YF7O that sounds fine to me. However, what if |
I clearly don't have the answer... If aws simply proposed an @types npm module for xray, that's all we could use... (and I wouldn't be here lol). But I must lack experience to answer this question, sorry :/ |
Not sure if this is just me but using this library with typescript has been painful, even following the documentation doesn't work. I think the functionality the library provides is very powerful but as it stands makes TS usage very hard.
|
@mihilmy our TypeScript support is 100% from the community. A PR to improve these areas you've identified would be more than welcomed. |
I just figured out what to do to make this work well with typescript. Problem export const handler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
const payload = event.body as unknown as { name: string };
//...
}
Solution //jsonBodyParser middleware
const {
headers,
body
} = request.event;
//...
const data = request.event.isBase64Encoded ? Buffer.from(body, 'base64').toString() : body;
request.event.rawBody = body;
request.event.body = JSON.parse(data, options.reviver); As you can see below, in my modified example original request data is not modified so I will be able to achieve type consistency across middlewares. //jsonBodyParser middleware
request.event.jsonBody = JSON.parse(request.event.isBase64Encoded ? Buffer.from(body, 'base64').toString() : body, options.reviver); It would also decrease amount of documentation which is needed for this package because you will not need to describe how package changes aws request but only how it extend it. Future export const handler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
const payload = event.jsonBody as { name: string };
//...
} As you can see it is still not so good. I belive we could remove export const handler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
const payload = event.jsonBody // payload type is equal to "{ name: string }"
//...
}
export const handle = middy(handler)
.use(jsonBodyParser<{ name: string }>()) Idea Also i suggest to make only one way of applying middlewares. Since now I can put middlewares into each Using functions from |
From my understanding, it is impossible to achieve this with a structure like
because
which allows a bit more type safety, as |
I can agree with @dbartholomae.
I also belive a compose functions are the way to go. @dbartholomae Would you mind telling us more about limits with approach you showed? |
Closing, but will keep pinned. |
@czlowiek488 For the lambda-middleware repo, you can find the discussion here: Basically, there are some limitations around generics (or were back then, I haven't checked yet with the latest TypeScript version) that limit type inference if allmiddlewares and the handler are generic. Basically if you want full type coverage, you need to write this export const handlerWithMiddleware = middleware1()(middleware2()(middleware3()(handler))) instead of this: export const handlerWithMiddleware = compose(middleware1(), middleware2(), middleware3())(handler) |
@willfarrell To make sure subsequent + declare type MiddlewareObjEvent<T extends MiddlewareObj> = T extends MiddlewareObj<infer U> ? U : never;
+ declare type UnwrapArray<T> = T extends Array<infer U> ? U : T;
declare type UseFn<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> =
(middlewares: MiddlewareObj<TEvent, TResult, TErr, TContext> | Array<MiddlewareObj<TEvent, TResult, TErr, TContext>>) => MiddyfiedHandler<
- TEvent
+ MiddlewareObjEvent<UnwrapArray<typeof middlewares>> & TEvent,
TResult, TErr, TContext>; EDIT: Added examples middy(handler)
.use(foo()) // FooEvent
.use(bar()) // FooEvent & BarEvent
.use(baz()); // FooEvent & BarEvent & BazEvent |
Please see: #1023 |
TypeScript support is made up 100% from community contributions. None of the core maintainers know or use TypeScript and thus are unable to offer support for it. We believe that TypeScript has value to some and are willing to merge in changes the community find valuable.
If you would like to make Middy better, please consider submitting, reviewing, and/or commenting on PRs/issues. We generally like to give a week before merging a PR to give the community time to comment.
TypeScript Issues
The text was updated successfully, but these errors were encountered: