-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
chore: improve d.ts with ts support #2306
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好像有一个测试 fixture 里面的用法改改?
@atian25 app-ts 那个 fixtures 里就这么一句 export default {
keys: 'foo',
} 没用到 EggAppConfig |
嗯,那就只是 example 那边错? |
嗯,就 example 那边之后改一下就好了 |
index.d.ts
Outdated
@@ -165,6 +165,15 @@ declare module 'egg' { | |||
|
|||
export type LoggerLevel = 'DEBUG' | 'INFO' | 'WARN' | 'ERROR' | 'NONE'; | |||
|
|||
export interface EggAppInfo { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
顶部加个. jsdoc?说明下这个用在哪里的,如给个 config 的示例
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
加了
Codecov Report
@@ Coverage Diff @@
## master #2306 +/- ##
=======================================
Coverage 99.59% 99.59%
=======================================
Files 29 29
Lines 749 749
=======================================
Hits 746 746
Misses 3 3 Continue to review full report at Codecov.
|
index.d.ts
Outdated
* ``` | ||
*/ | ||
export interface EggAppInfo { | ||
pkg: string; // package.json |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里错了, pkg 是 json
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
哦,我改一下
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rebase
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
改了
* } | ||
* ``` | ||
*/ | ||
export interface EggAppInfo { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个哪里有文档么?我当时看着文档写的,这些地段哪里那的?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shepherdwind 之前你们 example 那边写的 config 理解有问题。
export default (appInfo: EggAppInfo, appConfig?: EggAppConfig) => {}
这才是 config.{env}.js
的签名
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不过 appConfig 这个没几个人知道,哈哈
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
顺手改下 logger 那里
index.d.ts
Outdated
env?: EggEnvType; | ||
path?: string; | ||
package?: string; | ||
enabled?: boolean; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
enable
index.d.ts
Outdated
/** | ||
* build-in plugin list | ||
*/ | ||
interface EggPluginList { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
就叫 EggPlugin 吧
index.d.ts
Outdated
@@ -348,11 +371,31 @@ declare module 'egg' { | |||
|
|||
siteFile: any; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/eggjs/egg/blob/master/config/config.default.js#L167
应该是 [key: string]: string | buffer
* ``` | ||
*/ | ||
export interface EggAppInfo { | ||
pkg: any; // package.json |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
是个 object
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
看到 config 下的就是 any,这种 package info,特意写成 { [key: string]: any } 意义不大啊
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any 可以是 Array,String 啥的吧?不过这个无所谓了。
index.d.ts
Outdated
cache: boolean; | ||
defaultExtension: string; | ||
defaultViewEngine: string; | ||
mapping: any; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
object, key value 都是 string
index.d.ts
Outdated
watcher: any; | ||
watcher: { | ||
type: string; | ||
eventSources: { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个 key 可以干掉,或者里面允许 [key: string]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
感觉这些是不是提交到 egg-watcher 那边,然后 egg 里面 import 就好了?不要都写在 egg 里面
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个就得所有插件一个一个改了,暂时先写在 egg 里把?之后再给那些插件加
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
先在这改吧,改完我们看看是不是再一个 PR 干掉,然后才一起发布。
index.d.ts
Outdated
}; | ||
|
||
// egg env type | ||
type EggEnvType = 'local' | 'unittest' | 'prod'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
加个注释,告诉开发者如何覆盖
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个覆盖就是 declare merging....这个不需要写在注释里把,应该是要写在如何写插件或者框架的 ts 那里吧?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果这个都要写,那 interface 那些不也一样要写了么...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
行吧,那在 ts 文档那边写就好了,或者是在 d.ts 的顶部写一次。
index.d.ts
Outdated
@@ -540,6 +594,8 @@ declare module 'egg' { | |||
controller: IController; | |||
|
|||
Controller: Controller; | |||
|
|||
middlewares: IMiddleware; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
用 middleware
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
egg-core 里是 middlewares 哦
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
你往上看看,有一个 defineProperty
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
哦,看到了,我改一下
index.d.ts
Outdated
|
||
static: any; | ||
static: { | ||
prefix: string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
除了这些还有其他参数的,https://github.com/eggjs/egg-static#configuration
加个允许未知 key 吧
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好,我加个 &PlainObject
* // { view: { defaultEngines: string } } => { view?: { defaultEngines?: string } } | ||
* type EggConfig = PowerPartial<EggAppConfig> | ||
*/ | ||
type PowerPartial<T> = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个 PowerPartial 的实现,用递归写法好点吧 ?
type PowerPartial<T> = {
[U in keyof T]? : T[U] extends {}
? PowerPartial<T[U]>
: T[U];
};
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
可以,我改一下
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
会不会有性能问题。。。这样一直递归下去,如果是按照此前的,就至少能控制在 3 层
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
哇,递归后就是无限层了?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
额,你这样讲也有可能,不过这个只是对编译期,应该还好吧~~看看翰文他们什么意见。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
interface Test {
a: {
a1: {
a2: {
a3: Test;
};
},
};
b: string;
}
type TestP = PowerPartial<Test>;
const t: TestP = {
a: {
a1: {
a2: {
a3: {
a: {
},
},
},
},
},
b: 'b',
};
试了下,编译提示都没有什么问题,也没有卡顿,ts应该有这种内部保护机制吧……
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不是对象,只是 interface 的话,是可能有的有这种互相引用的情况的,不过我刚才试了一下,发现没问题,那我改成你这种把
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
先不改吧,反正 config 一般也就三层
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我能说我现在就有4层的么……😆
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK,改了
@@ -3,8 +3,23 @@ import * as KoaApplication from 'koa'; | |||
import * as KoaRouter from 'koa-router'; | |||
import { RequestOptions } from 'urllib'; | |||
import { Readable } from 'stream'; | |||
import 'egg-onerror'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
插件全部都引入么?但是如果用户不开启也会有相关类型提示了,会不会太多了。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这些都是 egg 内置的插件,其实是希望之后,能把配置这些分散到各个插件去实现
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这些插件都是开启的吧,目前这些插件也还没有 d.ts,现在先引入,之后补了插件的 d.ts 之后,这个也可以直接生效了
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我让他先写入的,后面 egg 里面的一些如 config.development 的定义,都下沉到插件那边去。
现在先 import 了,避免后面又要发 egg 版本
chore: improve d.ts with ts support (#2306)
Checklist
npm test
passesAffected core subsystem(s)
Description of change