Git Product home page Git Product logo

inject-js's People

Contributors

biyingshuai avatar cheer4chai avatar harttle avatar semantic-release-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

inject-js's Issues

给 Token、Provider 更强的类型约束

目前 Token 可以在使用时随意指定,可能会冲突,也可能不匹配 Provider 提供的具体 Service 类型。

  • inject-js 提供 Token。可以不依赖 Symbol API,fallback 到 nubmer 或 string。
  • 约束 Token 和 Provider 匹配。目测利用泛型参数可以做到,Token 和 Provider 搭配错的使用情况应该报类型错误。

此外,还应该提倡编写 provider 的地方,同时应该提供对应的 token。使用时一起引入,比如类似这样:

// 定义处
class QueryService {}
export QUERY_TOKEN = createToken<QueryService>()
export function queryFactory(): QueryService { return new QueryService(); }
// 使用处
import { QUERY_TOKEN, queryFactory } from './query.factory'
container.addFactory(QUERY_TOKEN, queryFactory)

怎样支持用接口作为token

依赖注入的用处在于,依赖接口而非具体的类。但目前 token 只能是 class,或者手动指定的 Symbol、string,问题是:

  • 如果依赖中指定的是具体的类就违反了初衷:注入的只能是子类,而不是实现了接口的类。
  • 如果依赖中指定的是 symbol 或者 string 就没有用到 TypeScript 提供的便利:还是没有实现完全自动注入,需要手动指定 token。

如果支持接口本身作为token就可以解决这些问题。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.