Comments (11)
Wait for microsoft/TypeScript#5453
from almin.
Can we make it?
type UseCases = AUseCase | BUseCase;
const context = new Context<UseCases>();
context.useCase(new AUseCase).execute(1,2,3);
from almin.
Status 2017-03-29
I have a step-by-step plan to resolve this issue.
Step 1. Add generics to context.useCase<UseCaseArgs>(useCase).execute(arg)
#137
The user can write following:
type ParentUseCaseArgs = string;
class ParentUseCase extends UseCase {
execute(value: ParentUseCaseArgs) {
return this.context.useCase(new ChildUseCase()).execute(value);
}
}
context.useCase(parentUseCase).execute<ParentUseCaseArgs>("arg").then(() => {
const state = context.getState<StoreState>();
console.log(state.A.a);
console.log(state.B.b);
}).catch((error: Error) => {
console.error(error);
});
But, it is lengthy syntax.
Step 2: if TypeScript implement Variadic Kinds, We can move to that.
The user can write following:
class ParentUseCase extends UseCase {
execute(value: string) {
return this.context.useCase(new ChildUseCase()).execute(value);
}
}
context.useCase(parentUseCase).execute("arg").then(() => {
const state = context.getState<StoreState>();
console.log(state.A.a);
console.log(state.B.b);
}).catch((error: Error) => {
console.error(error);
});
from almin.
A current workaround for the variadic kinds I think of is something like the following.
interface UseCaseBase {
// define UseCase's methods and properties other than execute
foo(): string;
}
interface UseCase0 extends UseCaseBase {
execute(): any;
}
interface UseCase1<T1> extends UseCaseBase {
execute(arg1: T1): any;
}
interface UseCase2<T1, T2> extends UseCaseBase {
execute(arg1: T1, arg2: T2): any;
}
// ... create UseCaseX as many as you want
interface UseCaseConstructor {
new (): UseCase0;
new<T1> (): UseCase1<T1>;
new<T1, T2> (): UseCase2<T1, T2>;
}
// Implementation of UseCase
abstract class _UseCase {
abstract execute(...args: any[]): any;
foo() {
return '';
}
}
// Cast UseCase
const UseCase = _UseCase as UseCaseConstructor;
class SomeUseCase extends UseCase {
execute() {}
}
class AnotherUseCase extends UseCase<string> {
execute(arg1: string) {}
}
class YetAnotherUseCase extends UseCase<string, number> {
execute(arg1: string, arg2: number) {}
}
class Context {
useCase(useCase: UseCase0): { execute(): Promise<void> };
useCase<T1>(useCase: UseCase1<T1>): { execute(arg1: T1): Promise<void> };
useCase<T1, T2>(useCase: UseCase2<T1, T2>): { execute(arg1: T1, arg2: T2): Promise<void> };
useCase(useCase: { execute(...args: any[]): any }): { execute(...args: any[]): Promise<void> } {
// todo implement
}
}
What do you think of it?
from almin.
@kimamula Thanks for interesting idea.
from almin.
Land Variadic types on Roadmap(future).
from almin.
Currently, almin supports executor
method #193
This executor
is type-safe method.
executor(executor: (useCase: Pick<T, "execute">) => any): Promise<void>;
Example:
context.useCase(new MyUseCase())
.executor(useCase => useCase.execute("value"))
.then(() => {
console.log("test");
});
However, executor
is redundancy.
We will continue to investigate this issue.
from almin.
Maybe, We can realive execute()
typing by conditional types.
// rough note
export interface UseCase<T>{
execute(args: T);
}
type ResolveUseCaseArgType<T> =
T extends UseCase<infer R> ? R : never;
class Context {
useCase<T extends UseCase>(useCase: T): UseCaseExecutor<T>;
type UseCaseArgs = ResolveUseCaseArgType<T>;
return new UseCaseExecutor<UseCaseArgs>(useCase);
}
}
- Conditional types by ahejlsberg · Pull Request #21316 · Microsoft/TypeScript
- Type inference in conditional types by ahejlsberg · Pull Request #21496 · Microsoft/TypeScript
- Predefined conditional types by ahejlsberg · Pull Request #21847 · Microsoft/TypeScript
- TypeScript 2.8 の Conditional Types について - Qiita
from almin.
#107 (comment) can not be realized.
We can do?
https://github.com/almin/almin-thinking/blob/master/02-UseCase/04-Conditional-UseCase.ts
from almin.
feat(almin): Support
context.useCase#execute
typing #342
I've implemented type-check of execute
method in #342
But, it is has a drawback again to executor
method.
It it a limitation of execute
typing.
executor()
import { UseCase, Context } from "almin";
class MyUseCaseA extends UseCase {
execute(_a: string) {}
}
const context = new Context({
store: createStore({ name: "test" })
});
// valid
context.useCase(new MyUseCaseA()).executor(useCase => useCase.execute("A"));
// invalid
context.useCase(new MyUseCaseA()).executor(useCase => useCase.execute()); // no argument
context.useCase(new MyUseCaseA()).executor(useCase => useCase.execute(1)); // can not pass number
context.useCase(new MyUseCaseA()).executor(useCase => useCase.execute("A", 42)); // require 1 argument, but specify 2 arguments
execute()
import { UseCase, Context } from "almin";
class MyUseCaseA extends UseCase {
execute(_a: string) {}
}
const context = new Context({
store: createStore({ name: "test" })
});
// valid
context.useCase(new MyUseCaseA()).execute("A");
// invalid
context.useCase(new MyUseCaseA()).execute(); // no argument
context.useCase(new MyUseCaseA()).execute(1); // can not pass number
// **valid in almin 0.17**
// BUT, It should be error
context.useCase(new MyUseCaseA()).execute("A". 42);
from almin.
TypeScript 3.0 will work for execute
method completely.
Extracting and spreading parameter lists with tuples
https://blogs.msdn.microsoft.com/typescript/2018/07/12/announcing-typescript-3-0-rc/#tuples-and-parameters
from almin.
Related Issues (20)
- AlminPerfMarker should not throw error HOT 7
- Almin: Store#receivePayload design HOT 5
- almin-react-container: Convert tests to TypeScript
- Render props for Almin + React HOT 8
- LifeCycleEventHub#onDispatch receive StoreChangedPayload
- almin: remove `dispatcher` from `DispatcherPayloadMeta` HOT 1
- Almin: `transaction` property in Meta should be readonly HOT 2
- almin: use `assert` function instead of Node "assert" module
- Docs: warning as document
- Almin 0.17
- @almin/store-test-helper
- Drop to use `events`
- Support module field
- Deprecate `executor()`
- Almin 0.18 CHANGELOG
- Lerna 3 bug and workaround HOT 1
- Retire zuul HOT 2
- CI: GitHub Actions
- Action Required: Fix Renovate Configuration HOT 1
- Support React Hooks
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from almin.