Giter VIP home page Giter VIP logo

Comments (11)

azu avatar azu commented on May 15, 2024

Wait for microsoft/TypeScript#5453

from almin.

azu avatar azu commented on May 15, 2024

Can we make it?

type UseCases = AUseCase | BUseCase;
const context = new Context<UseCases>();
context.useCase(new AUseCase).execute(1,2,3);

from almin.

azu avatar azu commented on May 15, 2024

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.

kimamula avatar kimamula commented on May 15, 2024

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
    }
}

http://bit.ly/2opX4l2

What do you think of it?

from almin.

azu avatar azu commented on May 15, 2024

@kimamula Thanks for interesting idea.

from almin.

azu avatar azu commented on May 15, 2024

Land Variadic types on Roadmap(future).

from almin.

azu avatar azu commented on May 15, 2024

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");
 });

📝 Document: https://almin.js.org/docs/en/usecaseexecutor-api.html#executorexecutor-usecase-pick-t-execute-any-promise-void

However, executor is redundancy.
We will continue to investigate this issue.

from almin.

azu avatar azu commented on May 15, 2024

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);
    }
}

from almin.

azu avatar azu commented on May 15, 2024

#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.

azu avatar azu commented on May 15, 2024

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.

azu avatar azu commented on May 15, 2024

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)

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.