Skip to content
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

docs: add TypeScript Guide #2324

Merged
merged 4 commits into from
Apr 12, 2018
Merged

docs: add TypeScript Guide #2324

merged 4 commits into from
Apr 12, 2018

Conversation

atian25
Copy link
Member

@atian25 atian25 commented Apr 6, 2018

Checklist
  • npm test passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)
Description of change

@codecov
Copy link

codecov bot commented Apr 6, 2018

Codecov Report

Merging #2324 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #2324   +/-   ##
=======================================
  Coverage   99.61%   99.61%           
=======================================
  Files          29       29           
  Lines         773      773           
=======================================
  Hits          770      770           
  Misses          3        3

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cd418f5...adab22a. Read the comment docs.

}

// 将 Egg 整个 export 出去
export = Egg;
Copy link
Member

@popomore popomore Apr 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里应该继承 egg 再 export 吧?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

要的,否则框架继承会有问题, @whxaxes 来解释下吧

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

使用 export = ,就相当于完全将 egg 导出去,import 上层框架的时候也能引入 egg 的类型,如果在插件中通过 declare module 'egg' 拓展也能够 merge 进上层框架。但是如果是 export * from 'egg' ,测试过发现会没法 merge 进去

Copy link
Member

@popomore popomore Apr 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这样写也会有问题的,这里要实现继承,比如自定义了 startCluster,并增加了一个参数,那这里其实就不是 Egg 的了。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不过可以放在 Tegg 统一考虑。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果要自定义,就直接在上层框架中,比如我的上层框架叫 larva,就直接在 larva 的 d.ts 中

...

export = Egg;

declare module 'larva' {
  export function startCluster(xxx): void;
}

我在我们团队的上层框架就是这么实现的,暂时还没遇到什么问题。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果这样定义,这句就不需要了吧 export = Egg;,都是通过 import * as Larva from 'larva'

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

需要啊,如果没有这句

import { EggAppConfig } from 'larva'

这样写就出错了

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import * as Larva from 'larva'
const EggAppConfig = Larva.EggAppConfig;

module 的意思就是 commonjs 的方式,而上面的写法其实是 es module。所以上面的 export 是很奇怪的。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是的,官方文档也是这么说,不过经过各种尝试,再看到 power-assert 他们的 typings 也是这么写的,将 export = node 的 assert ,自己试了一下发现确实这样可以实现多层框架拓展的问题,所以就这么写了

@dead-horse
Copy link
Member

英文的可以先 google translate 一下

@atian25
Copy link
Member Author

atian25 commented Apr 9, 2018

英文的我让 方正 来翻译

@popomore
Copy link
Member

@atian25 没问题合了吧

@atian25 atian25 merged commit f0e7773 into master Apr 12, 2018
@atian25 atian25 deleted the docs-ts branch April 12, 2018 02:15
popomore pushed a commit that referenced this pull request Apr 12, 2018
docs: add TypeScript Guide (#2324)
@atian25 atian25 mentioned this pull request Apr 12, 2018
28 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants