Why is my inital state of the reducer giving a type error when using interfaces?












0















How do I use interfaces combined with the initial state of my reducer in angular 6? When I use interfaces combined with the initial state of my reducer I get the following error:




ERROR in src/app/core/store/event/event.reducer.ts(8,14): error
TS2322: Type '{ loading: false; loaded: false; failed: false; data:
undefined; selectedEvent: {}; }' is not assignable to type 'Events'.
Types of property 'selectedEvent' are incompatible.
Type '{}' is not assignable to type 'Event'.
Property 'id' is missing in type '{}'.




event.interface.ts



export interface Event {
id: string;
title: string;
date: string;
place: string;
line: string;
type: string;
vehicle: string;
matriculeNr: string;
filterCTX: string;
status: string;
recordExisting: string;
mode: string;
module: string;
object: string;
eventId: string;
sharedObjects: SharedObject;
eventTime: string;
eventCodeDescr: string;
eventCodeDescrCorp: string;
description: string;
selectedAsMain: boolean;
selectedAsDetail: boolean;
selected: boolean;
documents: Document;
participants: Participant;
}

export interface Events {
loading: boolean;
loaded: boolean;
failed: boolean;
data: Array<Event>;
selectedEvent: Event;
}

export interface Document {
id: string;
name: string;
docLink: string;
}

export interface Participant {
id: string;
relation: string;
firstName: string;
lastName: string;
updateUser: string;
updateDate: string;
}

export interface SharedObject {
id: string;
}


event.reducer.ts



import * as events from './event.actions';
import { tassign } from 'tassign';
import { Events } from '@app/core/interfaces/event.interface';

/**
* Initial state for events
*/
export const EVENTS_INITIAL_STATE: Events = {
loading: false,
loaded: false,
failed: false,
data: ,
selectedEvent: {},
};

/**
* Case functions
*/
function load(state: Events): Events {
return tassign(state, { loading: true });
}

function loadSuccess(state: Events, action: events.LoadSuccess): Events {
return tassign(state, {
loaded: true,
loading: false,
failed: false,
data: action.payload,
selectedEvent: {},
});
}

function loadFail(state: Events): Events {
return tassign(state, {
loaded: false,
loading: false,
failed: true,
data: ,
selectedEvent: {},
});
}

function selectMainEvent(
state: Events,
action: events.SelectMainEvent
): Events {
const updatedData = state.data.map(event => {
if (event.id === action.payload) {
return {
...event,
selectedAsMain: true
};
}
return {
...event,
selectedAsMain: false
};
});

return tassign(state, {
loaded: true,
loading: false,
failed: false,
data: updatedData,
selectedEvent: {},
});
}

function selectDetailEvent(
state: Events,
action: events.SelectDetailEvent
): Events {
const updatedData = state.data.map(event => {
if (event.id === action.payload.eventId) {
return {
...event,
selectedAsDetail: true
};
}
return {
...event,
selectedAsDetail: false
};
});

return tassign(state, {
loaded: true,
loading: false,
failed: false,
data: updatedData,
selectedEvent: action.payload,
});
}

/**
* EventsReducer
*
* @param state
* @param action
*/
export function reducer(
state: Events = EVENTS_INITIAL_STATE,
action: events.Actions
): Events {
switch (action.type) {
case events.LOAD:
return load(state);
case events.LOAD_SUCCESS:
return loadSuccess(state, action);
case events.LOAD_FAIL:
return loadFail(state);
case events.MAIN_EVENT_SELECTED:
return selectMainEvent(state, action);
case events.DETAIL_EVENT_SELECTED:
return selectDetailEvent(state, action);
}

return state;
}

/**
* Selectors
*/
export const getData = (state: Events) => state.data;
export const getSelectedEvents = (state: Events) =>
state.data.filter(event => event.selected);
export const getSelectedDetailEvent = (state: Events) => state.selectedEvent;
export const getLoading = (state: Events) => state.loading;
export const getLoaded = (state: Events) => state.loaded;
export const getFailed = (state: Events) => state.failed;









share|improve this question























  • in interface Events you have this: selectedEvent: Event; in definition you have selectedEvent: {},... type Event and {} is not the same... the error you get exactly said that... you can change interface with this "selectedEvent: Event | {};"

    – Juraj Kocan
    17 hours ago













  • @incNick no you cant until you define interface item as nullable selectedEvent: Event | null

    – Juraj Kocan
    17 hours ago











  • @JurajKocan Ooo, ha, thank you very much :)

    – incNick
    16 hours ago
















0















How do I use interfaces combined with the initial state of my reducer in angular 6? When I use interfaces combined with the initial state of my reducer I get the following error:




ERROR in src/app/core/store/event/event.reducer.ts(8,14): error
TS2322: Type '{ loading: false; loaded: false; failed: false; data:
undefined; selectedEvent: {}; }' is not assignable to type 'Events'.
Types of property 'selectedEvent' are incompatible.
Type '{}' is not assignable to type 'Event'.
Property 'id' is missing in type '{}'.




event.interface.ts



export interface Event {
id: string;
title: string;
date: string;
place: string;
line: string;
type: string;
vehicle: string;
matriculeNr: string;
filterCTX: string;
status: string;
recordExisting: string;
mode: string;
module: string;
object: string;
eventId: string;
sharedObjects: SharedObject;
eventTime: string;
eventCodeDescr: string;
eventCodeDescrCorp: string;
description: string;
selectedAsMain: boolean;
selectedAsDetail: boolean;
selected: boolean;
documents: Document;
participants: Participant;
}

export interface Events {
loading: boolean;
loaded: boolean;
failed: boolean;
data: Array<Event>;
selectedEvent: Event;
}

export interface Document {
id: string;
name: string;
docLink: string;
}

export interface Participant {
id: string;
relation: string;
firstName: string;
lastName: string;
updateUser: string;
updateDate: string;
}

export interface SharedObject {
id: string;
}


event.reducer.ts



import * as events from './event.actions';
import { tassign } from 'tassign';
import { Events } from '@app/core/interfaces/event.interface';

/**
* Initial state for events
*/
export const EVENTS_INITIAL_STATE: Events = {
loading: false,
loaded: false,
failed: false,
data: ,
selectedEvent: {},
};

/**
* Case functions
*/
function load(state: Events): Events {
return tassign(state, { loading: true });
}

function loadSuccess(state: Events, action: events.LoadSuccess): Events {
return tassign(state, {
loaded: true,
loading: false,
failed: false,
data: action.payload,
selectedEvent: {},
});
}

function loadFail(state: Events): Events {
return tassign(state, {
loaded: false,
loading: false,
failed: true,
data: ,
selectedEvent: {},
});
}

function selectMainEvent(
state: Events,
action: events.SelectMainEvent
): Events {
const updatedData = state.data.map(event => {
if (event.id === action.payload) {
return {
...event,
selectedAsMain: true
};
}
return {
...event,
selectedAsMain: false
};
});

return tassign(state, {
loaded: true,
loading: false,
failed: false,
data: updatedData,
selectedEvent: {},
});
}

function selectDetailEvent(
state: Events,
action: events.SelectDetailEvent
): Events {
const updatedData = state.data.map(event => {
if (event.id === action.payload.eventId) {
return {
...event,
selectedAsDetail: true
};
}
return {
...event,
selectedAsDetail: false
};
});

return tassign(state, {
loaded: true,
loading: false,
failed: false,
data: updatedData,
selectedEvent: action.payload,
});
}

/**
* EventsReducer
*
* @param state
* @param action
*/
export function reducer(
state: Events = EVENTS_INITIAL_STATE,
action: events.Actions
): Events {
switch (action.type) {
case events.LOAD:
return load(state);
case events.LOAD_SUCCESS:
return loadSuccess(state, action);
case events.LOAD_FAIL:
return loadFail(state);
case events.MAIN_EVENT_SELECTED:
return selectMainEvent(state, action);
case events.DETAIL_EVENT_SELECTED:
return selectDetailEvent(state, action);
}

return state;
}

/**
* Selectors
*/
export const getData = (state: Events) => state.data;
export const getSelectedEvents = (state: Events) =>
state.data.filter(event => event.selected);
export const getSelectedDetailEvent = (state: Events) => state.selectedEvent;
export const getLoading = (state: Events) => state.loading;
export const getLoaded = (state: Events) => state.loaded;
export const getFailed = (state: Events) => state.failed;









share|improve this question























  • in interface Events you have this: selectedEvent: Event; in definition you have selectedEvent: {},... type Event and {} is not the same... the error you get exactly said that... you can change interface with this "selectedEvent: Event | {};"

    – Juraj Kocan
    17 hours ago













  • @incNick no you cant until you define interface item as nullable selectedEvent: Event | null

    – Juraj Kocan
    17 hours ago











  • @JurajKocan Ooo, ha, thank you very much :)

    – incNick
    16 hours ago














0












0








0








How do I use interfaces combined with the initial state of my reducer in angular 6? When I use interfaces combined with the initial state of my reducer I get the following error:




ERROR in src/app/core/store/event/event.reducer.ts(8,14): error
TS2322: Type '{ loading: false; loaded: false; failed: false; data:
undefined; selectedEvent: {}; }' is not assignable to type 'Events'.
Types of property 'selectedEvent' are incompatible.
Type '{}' is not assignable to type 'Event'.
Property 'id' is missing in type '{}'.




event.interface.ts



export interface Event {
id: string;
title: string;
date: string;
place: string;
line: string;
type: string;
vehicle: string;
matriculeNr: string;
filterCTX: string;
status: string;
recordExisting: string;
mode: string;
module: string;
object: string;
eventId: string;
sharedObjects: SharedObject;
eventTime: string;
eventCodeDescr: string;
eventCodeDescrCorp: string;
description: string;
selectedAsMain: boolean;
selectedAsDetail: boolean;
selected: boolean;
documents: Document;
participants: Participant;
}

export interface Events {
loading: boolean;
loaded: boolean;
failed: boolean;
data: Array<Event>;
selectedEvent: Event;
}

export interface Document {
id: string;
name: string;
docLink: string;
}

export interface Participant {
id: string;
relation: string;
firstName: string;
lastName: string;
updateUser: string;
updateDate: string;
}

export interface SharedObject {
id: string;
}


event.reducer.ts



import * as events from './event.actions';
import { tassign } from 'tassign';
import { Events } from '@app/core/interfaces/event.interface';

/**
* Initial state for events
*/
export const EVENTS_INITIAL_STATE: Events = {
loading: false,
loaded: false,
failed: false,
data: ,
selectedEvent: {},
};

/**
* Case functions
*/
function load(state: Events): Events {
return tassign(state, { loading: true });
}

function loadSuccess(state: Events, action: events.LoadSuccess): Events {
return tassign(state, {
loaded: true,
loading: false,
failed: false,
data: action.payload,
selectedEvent: {},
});
}

function loadFail(state: Events): Events {
return tassign(state, {
loaded: false,
loading: false,
failed: true,
data: ,
selectedEvent: {},
});
}

function selectMainEvent(
state: Events,
action: events.SelectMainEvent
): Events {
const updatedData = state.data.map(event => {
if (event.id === action.payload) {
return {
...event,
selectedAsMain: true
};
}
return {
...event,
selectedAsMain: false
};
});

return tassign(state, {
loaded: true,
loading: false,
failed: false,
data: updatedData,
selectedEvent: {},
});
}

function selectDetailEvent(
state: Events,
action: events.SelectDetailEvent
): Events {
const updatedData = state.data.map(event => {
if (event.id === action.payload.eventId) {
return {
...event,
selectedAsDetail: true
};
}
return {
...event,
selectedAsDetail: false
};
});

return tassign(state, {
loaded: true,
loading: false,
failed: false,
data: updatedData,
selectedEvent: action.payload,
});
}

/**
* EventsReducer
*
* @param state
* @param action
*/
export function reducer(
state: Events = EVENTS_INITIAL_STATE,
action: events.Actions
): Events {
switch (action.type) {
case events.LOAD:
return load(state);
case events.LOAD_SUCCESS:
return loadSuccess(state, action);
case events.LOAD_FAIL:
return loadFail(state);
case events.MAIN_EVENT_SELECTED:
return selectMainEvent(state, action);
case events.DETAIL_EVENT_SELECTED:
return selectDetailEvent(state, action);
}

return state;
}

/**
* Selectors
*/
export const getData = (state: Events) => state.data;
export const getSelectedEvents = (state: Events) =>
state.data.filter(event => event.selected);
export const getSelectedDetailEvent = (state: Events) => state.selectedEvent;
export const getLoading = (state: Events) => state.loading;
export const getLoaded = (state: Events) => state.loaded;
export const getFailed = (state: Events) => state.failed;









share|improve this question














How do I use interfaces combined with the initial state of my reducer in angular 6? When I use interfaces combined with the initial state of my reducer I get the following error:




ERROR in src/app/core/store/event/event.reducer.ts(8,14): error
TS2322: Type '{ loading: false; loaded: false; failed: false; data:
undefined; selectedEvent: {}; }' is not assignable to type 'Events'.
Types of property 'selectedEvent' are incompatible.
Type '{}' is not assignable to type 'Event'.
Property 'id' is missing in type '{}'.




event.interface.ts



export interface Event {
id: string;
title: string;
date: string;
place: string;
line: string;
type: string;
vehicle: string;
matriculeNr: string;
filterCTX: string;
status: string;
recordExisting: string;
mode: string;
module: string;
object: string;
eventId: string;
sharedObjects: SharedObject;
eventTime: string;
eventCodeDescr: string;
eventCodeDescrCorp: string;
description: string;
selectedAsMain: boolean;
selectedAsDetail: boolean;
selected: boolean;
documents: Document;
participants: Participant;
}

export interface Events {
loading: boolean;
loaded: boolean;
failed: boolean;
data: Array<Event>;
selectedEvent: Event;
}

export interface Document {
id: string;
name: string;
docLink: string;
}

export interface Participant {
id: string;
relation: string;
firstName: string;
lastName: string;
updateUser: string;
updateDate: string;
}

export interface SharedObject {
id: string;
}


event.reducer.ts



import * as events from './event.actions';
import { tassign } from 'tassign';
import { Events } from '@app/core/interfaces/event.interface';

/**
* Initial state for events
*/
export const EVENTS_INITIAL_STATE: Events = {
loading: false,
loaded: false,
failed: false,
data: ,
selectedEvent: {},
};

/**
* Case functions
*/
function load(state: Events): Events {
return tassign(state, { loading: true });
}

function loadSuccess(state: Events, action: events.LoadSuccess): Events {
return tassign(state, {
loaded: true,
loading: false,
failed: false,
data: action.payload,
selectedEvent: {},
});
}

function loadFail(state: Events): Events {
return tassign(state, {
loaded: false,
loading: false,
failed: true,
data: ,
selectedEvent: {},
});
}

function selectMainEvent(
state: Events,
action: events.SelectMainEvent
): Events {
const updatedData = state.data.map(event => {
if (event.id === action.payload) {
return {
...event,
selectedAsMain: true
};
}
return {
...event,
selectedAsMain: false
};
});

return tassign(state, {
loaded: true,
loading: false,
failed: false,
data: updatedData,
selectedEvent: {},
});
}

function selectDetailEvent(
state: Events,
action: events.SelectDetailEvent
): Events {
const updatedData = state.data.map(event => {
if (event.id === action.payload.eventId) {
return {
...event,
selectedAsDetail: true
};
}
return {
...event,
selectedAsDetail: false
};
});

return tassign(state, {
loaded: true,
loading: false,
failed: false,
data: updatedData,
selectedEvent: action.payload,
});
}

/**
* EventsReducer
*
* @param state
* @param action
*/
export function reducer(
state: Events = EVENTS_INITIAL_STATE,
action: events.Actions
): Events {
switch (action.type) {
case events.LOAD:
return load(state);
case events.LOAD_SUCCESS:
return loadSuccess(state, action);
case events.LOAD_FAIL:
return loadFail(state);
case events.MAIN_EVENT_SELECTED:
return selectMainEvent(state, action);
case events.DETAIL_EVENT_SELECTED:
return selectDetailEvent(state, action);
}

return state;
}

/**
* Selectors
*/
export const getData = (state: Events) => state.data;
export const getSelectedEvents = (state: Events) =>
state.data.filter(event => event.selected);
export const getSelectedDetailEvent = (state: Events) => state.selectedEvent;
export const getLoading = (state: Events) => state.loading;
export const getLoaded = (state: Events) => state.loaded;
export const getFailed = (state: Events) => state.failed;






angular typescript






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 17 hours ago









Jim PeetersJim Peeters

78441128




78441128













  • in interface Events you have this: selectedEvent: Event; in definition you have selectedEvent: {},... type Event and {} is not the same... the error you get exactly said that... you can change interface with this "selectedEvent: Event | {};"

    – Juraj Kocan
    17 hours ago













  • @incNick no you cant until you define interface item as nullable selectedEvent: Event | null

    – Juraj Kocan
    17 hours ago











  • @JurajKocan Ooo, ha, thank you very much :)

    – incNick
    16 hours ago



















  • in interface Events you have this: selectedEvent: Event; in definition you have selectedEvent: {},... type Event and {} is not the same... the error you get exactly said that... you can change interface with this "selectedEvent: Event | {};"

    – Juraj Kocan
    17 hours ago













  • @incNick no you cant until you define interface item as nullable selectedEvent: Event | null

    – Juraj Kocan
    17 hours ago











  • @JurajKocan Ooo, ha, thank you very much :)

    – incNick
    16 hours ago

















in interface Events you have this: selectedEvent: Event; in definition you have selectedEvent: {},... type Event and {} is not the same... the error you get exactly said that... you can change interface with this "selectedEvent: Event | {};"

– Juraj Kocan
17 hours ago







in interface Events you have this: selectedEvent: Event; in definition you have selectedEvent: {},... type Event and {} is not the same... the error you get exactly said that... you can change interface with this "selectedEvent: Event | {};"

– Juraj Kocan
17 hours ago















@incNick no you cant until you define interface item as nullable selectedEvent: Event | null

– Juraj Kocan
17 hours ago





@incNick no you cant until you define interface item as nullable selectedEvent: Event | null

– Juraj Kocan
17 hours ago













@JurajKocan Ooo, ha, thank you very much :)

– incNick
16 hours ago





@JurajKocan Ooo, ha, thank you very much :)

– incNick
16 hours ago












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54250484%2fwhy-is-my-inital-state-of-the-reducer-giving-a-type-error-when-using-interfaces%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54250484%2fwhy-is-my-inital-state-of-the-reducer-giving-a-type-error-when-using-interfaces%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Callistus III

Ostreoida

Index Sanctorum