Closed michie1 closed 4 years ago
Neat...
ServerGeneric
? And what do you think of naming the type variables?
type WithoutContext<EndpointFunction, Context> =
EndpointFunction extends (this: Context, ...args: infer EndpointArguments) => infer EndpointResult
? (...args: EndpointArguments) => EndpointResult
: never;
export type ServerGeneric<Endpoints, Context> = { [endpointName in keyof Endpoints]: WithoutContext<Endpoints[endpointName], Context>; };
It's more verbose though. not sure which is better.
2. The `examples/typescript/tsconfig.json` doesn't have `"strict": true`. Setting `strict` solves this.
3. I just tried and seems to just work.
4. Check out the `docs/readme.tempate.md` and the `package.json#scripts.docs` script of the root `package.json`.
```md
## TypeScript
You can use your backend types on the frontend by using TypeScript's `typeof`.
~~~ts
!INLINE /examples/typescript/endpoints.ts
!INLINE /examples/typescript/client/index.ts
The `!INLINE` command inlines the content of the file; comfy to write always-updated and bug-free code blocks :).
5. I guess `tsc --noEmit` would do the trick? I tried and it seems to find the tsc files and transpile them (I still have to check if it actually finds all the `.tsc` files), even though `tsconfig.json` is pretty much empty. Maybe a `examples/typescript/package.json#scripts.test` that 1. installs the dependencies and 2. runs `tsc --noEmit`.
Thanks for your lovely solution & PR. It was making me sad that I couldn't find a solution.
ServerGeneric
, I don't know if calling it generic makes it more clear. Furthermore, it's not a big deal for me. Using larger names for the variables is a good idea.You are welcome. It's nice for me that I can contribute something back. The PR is not finished yet, but we'll get there.
Btw, I can't start the backend in the typescript example in Master.
/home/m/wildcard-api/server/WildcardServer.ts:338
assert(yes(() => {}));
Seems like there is a bug in the isArrowFunction
?
I suspect you are using another Node version. I'll rewrite a more stable version on master.
I suspect you are using another Node version. I'll rewrite a more stable version on master.
Done 80c00cabefc9eda180454cf2214c791d0959c785. Does it work now?
You're right it's redundant. Hm, not sure how to name this then. Maybe we can keep FrontendType
until we find something better.
Ok that makes a lot of sense; I'll add TS types to the tests.
Done 80c00ca. Does it work now?
The patch is published in 0.5.2
Done 80c00ca. Does it work now?
The patch is published in
0.5.2
I get the same assertion error with node v12.19.0 and latest master (bd7e3fab975321e0cbdc1257d2036b4f4d7404af
)
ts-node is converting the arrow function to a regular function.
console.log((() => {}).toString()); // function () { }
ts-node -e "console.log((() => {}).toString())"
tsc
is doing the same.
I've made some changes to the scripts.
Does the following now work?
git checkout master
git pull
cd examples/typescript/
yarn clean
yarn start
http://localhost:3000
That's precisely what I'm doing on my end and TS is not transpiling arrow functions, which is the expected behavior since tsconfig.json#compilerOptions.target==="es2017"
(the root tsconfig.json
).
Yes, that works, my bad. Thanks!
Ok neat - no probs. (Actually I'm glad I rewrote the scripts, they were suboptimal.)
Merged :ok_hand:.
I'm curious, you mentioned a while ago using getContext
to set cookies, are you still using that approach? What do you think of https://github.com/reframejs/wildcard-api/issues/59?
Resolves #51
FrontendType
,ClientType
?getPerson
Promise<Person>
and notPromise<Person | null>
, while typescript is also not complaining about dead code when we compare it tonull
?this
and are therefore not being typed.Not done in this PR: