logo
Tags down

shadow

How to initialise interface properties in complex interfaces


By : Kathleen Krueger
Date : October 14 2020, 02:21 PM
seems to work fine According to your typse IUserState has a property user that has those properties. You omitted that. Also IUserProps and IUserState are identical but you nest them which does not make logical sense. I removed IUserProps.
StackBlitz
code :
export interface IField {
    Label: string,
    Name: string,
    ErrorMessage: string,
    Text: string
}


export interface IUserState {
    user:{
        Email:IField,
        FirstName: IField 
    }  
}


export var setDefaultUserState:() => IUserState = () => ({
  user: {
    Email: {
      ErrorMessage: '',
      Label:'Email',
      Name:'Email',            
      Text: ''
    },       
    FirstName: {
      ErrorMessage: '',
      Label:'Email',
      Name:'Email',            
      Text: ''
    },
  }}
)


Share : facebook icon twitter icon

Implementing interface properties in interfaces?


By : Aya Samir
Date : March 29 2020, 07:55 AM
To fix this issue I would probably implement this with read-only properties. It better pratice for the client to add/remove/update items in an existing collection rather than replace the collection altogether.
code :
interface IReportParams
{
    IEnumerable<Guid> SelectedItems { get; }
    IEnumerable<StatusEnum> SelectedStatuses { get; }
}

class MvcReportParams : IReportParams
{
    public MvcReportParams()
    {
        SelectedItems = new Collection<Guid>();
        SelectedStatuses = new Collection<StatusEnum>();
    }

    public IEnumerable<Guid> SelectedItems { get; private set; }
    public IEnumerable<StatusEnum> SelectedStatuses { get; private set; }
}

class WpfReportParams : IReportParams
{
    public WpfReportParams()
    {
        SelectedItems = new ObservableCollection<Guid>();
        SelectedStatuses = new ObservableCollection<StatusEnum>();
    }

    public IEnumerable<Guid> SelectedItems { get; private set; }
    public IEnumerable<StatusEnum> SelectedStatuses { get; private set; }
}

Implementing interface properties in interfaces with a setter?


By : Joe Clark
Date : March 29 2020, 07:55 AM
it fixes the issue No, that would not be the correct way. You shouldn't have setters to Collections. If you need to add to your collection, implement an Add method or expose your property via IList or ICollection or a similar interface which supports the Add method instead.
Note that this can be accomplished using explicit implementations of the interface, where you are able to expose a different collection than that from the interface.
code :
public interface IReportParams {
    IEnumerable<StatusEnum> SelectedStatuses { get; }
}

public class WpfReportParams : IReportParams {
    private readonly ObservableCollection<StatusEnum> _SelectedStatuses;

    public WpfReportParams() {
        _SelectedStatuses = new ObservableCollection<StatusEnum>();
    }

    public ICollection<StatusEnum> SelectedStatuses {
        get { return _SelectedStatuses; }
    }

    IEnumerable<StatusEnum> IReportParams.SelectedStatuses {
        get { return SelectedStatuses; }
    }
}

Interface that extends either of two interfaces and adds some properties?


By : user1954770
Date : March 29 2020, 07:55 AM
seems to work fine An interface can't extends a union type unfortunately. You could use an intersection type instead:
code :
interface Button {
    action: () => void;
    href?: never;
}
interface Link {
    href: string;
    action?: never;
}

type Action =  (Button | Link) & {
    children: string;
    size?: 'large' | 'small';
    variant?: 'secondary';
}

let a: Action = {
    href: "",
    children: "",
    size: "large"
}


let a2: Action = {
    action: () => { },
    children: "",
    size: "large"
}


let a3: Action = { // error
    href: ""
    action: () => { },
    children: "",
    size: "large"
}

Create interface based on properties of other interfaces


By : user2911309
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , Aaron Beall's answer is close (sorry I can't comment yet), but it doesn't properly remove all the keys that extends object, so they'll be required by the final type as key: never which is not wanted.
The following snippet achieves the objective by reusing a type-level helper from the awesome tycho01/typical repo:
code :
export type NonMatchingPropNames<T, X> = { [K in keyof T]: T[K] extends X ? never : K }[keyof T];

export type NonMatchingProps<T, X> = Pick<T, NonMatchingPropNames<T, X>>;

type PrimitiveValuesOf<T> = NonMatchingProps<T, object>;

type A = PrimitiveValuesOf<Workflow & WorkflowVersion>;

const a: A = {
  name: '',
  ID: 0,
  versionID: 1,
  lastPublished: '',
  environmentID: 2
}; // OK

Implementing interface properties in interfaces


By : Dugan Murphy
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , I need to create a dll file which contains all the interfaces of the classes but doesn't contain any class. Because I use these interfaces for a lot of classes it's must be like that: , You should declare Test as
shadow
Privacy Policy - Terms - Contact Us © voile276.org