3 Star 0 Fork 0

mirrors_Rich-Harris / generated-types

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

Generated types

This is a demo of how you can use TypeScript's rootDirs option to create 'virtual modules' with types based on the code that you've written.

In this example, src/server.ts is typed so that you can only return data that can be serialized as JSON in order to be sent to the client. Normally, this would mean that you can't tell anything about the return value other than that it's a valid JSONObject, but it would be very helpful if src/client.ts had access to a Data type that reflected what's actually being returned.

We can do this! All we need to do is create a 'proxy' file — types/proxy-server.ts — that mirrors the source code, but removes the type from the function and just types the parameter instead, along with a types/generated-types.ts that exports a Data type based on the ReturnType of the function in our server.ts file.

image

The next version of SvelteKit has a more sophisticated version of this process that is triggered by a file watcher. The one caveat is that you need to save the server.ts file in order for the proxy to be generated — until you do, there'll be a type mismatch.

空文件

简介

取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/mirrors_Rich-Harris/generated-types.git
git@gitee.com:mirrors_Rich-Harris/generated-types.git
mirrors_Rich-Harris
generated-types
generated-types
main

搜索帮助

53164aa7 5694891 3bd8fe86 5694891