question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot find name when using action as type in method

See original GitHub issue

I am trying to use this awesome library with @ngxs/store. Everything works fine except the type signature on action methods. When trying to use a generated ts-action action Typescript shows the compile time error: Cannot find name 'ActionName'.

See this stackblitz as demo: https://stackblitz.com/edit/angular-azg9at

import { State, StateContext, Action, Selector } from '@ngxs/store';
import { action, payload } from 'ts-action';

export const CreateModelAction = action('[App] CREATE MODEL', payload<AppModel>());

export interface AppModel {
  id: number;
  name?: string;
}

export interface AppStateModel {
  models: AppModel[];
}

const DEFAULT_STATE: AppStateModel = {
  models: []
};

@State({
  name: 'app',
  defaults: DEFAULT_STATE
})
export class AppState {

  @Selector()
  static models(state: AppStateModel) {
    return state.models;
  }

  @Action(CreateModelAction)
  createModel(ctx: StateContext<AppStateModel>, action: CreateModelAction) { // <-- this is the error here
    ctx.patchState({
      models: [
        ...ctx.getState().models,
        action.payload
      ]
    })
  }
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
cartantcommented, May 2, 2018

Actually, if you are using TypeScript 2.8, the best way to solve the problem is with the new, built-in InstanceType type:

export const CreateModelAction = action('[App] CREATE MODEL', payload<AppModel>());
export type CreateModelActionType = InstanceType<typeof CreateModelActionUnion>;

...

@Action(CreateModelAction)
createModel(ctx: StateContext<AppStateModel>, action: CreateModelActionType) { ... }
1reaction
cartantcommented, May 2, 2018

I don’t think so, action has to return a constructor - not the constructed type.

Anyway, there are some things that are much easier now, with TypeScript 2.8, so revisiting the implementation is on my todo list (to which I’ll add a reference to your question). It’s quite likely there will be some simplification (and breaking changes).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular and Typescript: Can't find names - Error: cannot ...
Just use typescript 2.0 or higher and install @types/core-js with npm: ... Turns out some names changed with typings v1 vs the old...
Read more >
Process Builder Deployment Error: “We can't find an action ...
When I try to deploy my changes to another sandbox have same problem "Error:(1, 1) Catch_Same_Products (Action) - We can't find an action...
Read more >
TypeScript errors and how to fix them
A list of common TypeScript errors and how to fix them.
Read more >
Usage With TypeScript
This page provides specific details for each of the different APIs included in Redux Toolkit and how to type them correctly with TypeScript....
Read more >
TypeScript: JavaScript With Syntax For Types.
Let's take this incorrect JavaScript code, and see how TypeScript can catch mistakes in your editor. js. function compact ( ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found