Skip to content

[signalStore] How to refer to the type of a particular store? #4140

Answered by markostanimirovic
jits asked this question in Q&A
Discussion options

You must be logged in to vote

You can define the type of the SignalStore in the following way:

// users.store.ts

export const UsersStore = signalStore(
  withState({ users: [] as User[] })
);

export type UsersStore = InstanceType<typeof UsersStore>;

Note: Exported type has the same name as the UsersStore constant. This is not required, but when defined like this, imported UsersStore can be used as a type as well as value:

// 1) as a type:
import { UsersStore } from './users.store';

function doSomething(usersStore: UsersStore) {}

// 2) as a value:
import { UsersStore } from './users.store';

class UsersComponent {
  readonly usersStore = inject(UsersStore);
}

// 3) type + value:
import { UsersStore } from './users…

Replies: 2 comments 6 replies

Comment options

You must be logged in to vote
5 replies
@jits
Comment options

@jits
Comment options

@markostanimirovic
Comment options

@jits
Comment options

@jits
Comment options

Answer selected by markostanimirovic
Comment options

You must be logged in to vote
1 reply
@rainerhahnekamp
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants