Git Product home page Git Product logo

Comments (8)

LouizFC avatar LouizFC commented on May 29, 2024 1

My workaround would be passing an object with all the string literals like so:

//Over Simplified Version

// We have a "Factory" where we declare the shape of our ActionCreators
type Factory = {
  a: (name: string) => /** ActionCreator */ ()=> { type: name };
  b: (name: string) => /** ActionCreator */ ()=> { type: name };
};

// We have a object with the same keys as the factory where we pass the ActionTypes
type Names = {
  a: "A";
  b: "B";
};

// We have produce an object with the correct ActionCreators
type Result = {
  a: () =>{ type: "A" };
  b: () =>{ type: "B" };
};

// The shape of the function
function(factory): (names) => result

It introduces some boilerplate, but the types that it produces will have the correct literals.

from deox.

LouizFC avatar LouizFC commented on May 29, 2024 1

Sorry for being away. September was a busy month.

I will do a rewrite with the approach that you suggested soon as I get some time.

from deox.

the-dr-lazy avatar the-dr-lazy commented on May 29, 2024

Looks related to #66 (not exactly similar).
The main issue is string interpolation which generalizes the string literal type to string type. Do you find any solution for it?

from deox.

the-dr-lazy avatar the-dr-lazy commented on May 29, 2024

There are some workarounds for string literal type operators in microsoft/TypeScript#12940 and microsoft/TypeScript#6579.

from deox.

LouizFC avatar LouizFC commented on May 29, 2024

I think we could tweak createActionCreator to return a cleaner type, but I don't know if this would be a breaking change.

Should I experiment with it in that same branch or create a separate branch?

I think that ithe ActionCreator type should have the following signature:

interface ActionCreator<
  TAction extends string,
  TCallable extends (...args: any) => any = (...args: any) => any,
  TPayload = undefined,
  TMeta = undefined
> {
  (...args: Parameters<TCallable>): Action<TAction, TPayload, TMeta>;
  type: TAction;
  toString(): TAction;
}

from deox.

Spaubleit avatar Spaubleit commented on May 29, 2024

Hello. Maybe it is possible to implement this using Template Literal Types from Typescript 4.1+

from deox.

LouizFC avatar LouizFC commented on May 29, 2024

@Spaubleit Yes. At the time there was no recursive types nor template types on Typescript. I think implementing this should be quite easy at the current state. Thanks for bringing this issue to my attention

Right now I am using @reduxjs/toolkit slices, but a more strong typed alternative would be quite good

from deox.

LouizFC avatar LouizFC commented on May 29, 2024

I am closing this because I see no more benefit in implementing this. As much as the action type information looks nice in the linter, its often wrong (I say from experience because I implemented this in a separate fork). Also, see https://phryneas.de/redux-typescript-no-discriminating-union

from deox.

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.