frintjs / frint

Modular JavaScript framework for building scalable and reactive applications
https://frint.js.org/
MIT License
756 stars 37 forks source link

Define type of Container.get() return value with generic #416

Closed viacheslaff closed 6 years ago

viacheslaff commented 6 years ago

Following suggestion by @markvincze, I've changed Container.get() signature from get(name: string): any; to get<T>(name: string): T;.

This not only allows to leverage type safety of TypeScript, but requires to use it.

Previously Container type was optional

const console: Console = container.get('console');
console.log()

Now if you don't specify <Console> it'll default to {} (empty object type) and console.log() won't compile. This is not ideal but unfortunately I don't know the way to require generic type in TS. Nevertheless it achieves the goal of making type required, because without using the type you pretty much cannot use the value received from the container.

const console = container.get<Console>('console');
console.log() // works
const console = container.get('console');
console.log() // error TS2339: Property 'log' does not exist on type '{}'
codecov[bot] commented 6 years ago

Codecov Report

Merging #416 into master will not change coverage. The diff coverage is 100%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #416   +/-   ##
=======================================
  Coverage   97.74%   97.74%           
=======================================
  Files         111      111           
  Lines        4251     4251           
=======================================
  Hits         4155     4155           
  Misses         96       96
Impacted Files Coverage Δ
packages/frint/src/App.spec.ts 100% <100%> (ø) :arrow_up:
packages/frint/src/App.ts 99.35% <100%> (ø) :arrow_up:

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 8f412ae...532158e. Read the comment docs.