notomo / gesture.nvim

Mouse gesture plugin for neovim
MIT License
527 stars 2 forks source link

Simplify di #25

Closed notomo closed 5 years ago

notomo commented 5 years ago
class Hoge {
  constructor(protected readonly hogeProp: string) {}
}

class Foo {
  constructor(protected readonly fooProp: number) {}
}

interface Deps {
  Foo: Foo;
  Hoge: Hoge;
}

type DepsFuncs = {
  Hoge: { (...args: any[]): Hoge };
  Foo: { (...args: any[]): Foo };
};

const deps: DepsFuncs = {
  Hoge: (...args: any[]): Hoge => {
    return new Hoge("a");
  },
  Foo: (...args: any[]) => {
    return new Foo(3);
  }
};

const resolve = <K extends keyof Deps>(cls: K): Deps[K] => {
  return deps[cls]();
};
{
    "private": true,
    "name": "ditest",
    "version": "0.0.1",
    "description": "",
    "dependencies": {},
    "devDependencies": {
        "typescript": "3.5.1"
    },
    "scripts": {
        "build": "tsc -p tsconfig.json"
    }
}
$ npm run build

> ditest@0.0.1 build /home/vagrant/workspace/proto/typescript/ditest
> tsc -p tsconfig.json

index.ts:29:3 - error TS2322: Type 'Hoge | Foo' is not assignable to type 'Deps[K]'.
  Type 'Hoge' is not assignable to type 'Deps[K]'.
    Type 'Hoge' is not assignable to type 'Foo & Hoge'.
      Property 'fooProp' is missing in type 'Hoge' but required in type 'Foo'.

29   return deps[cls]();
     ~~~~~~~~~~~~~~~~~~~

  index.ts:6:15
    6   constructor(protected readonly fooProp: number) {}
                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    'fooProp' is declared here.

Found 1 error.

🤔