rimeto/ts-optchain

Checking sum types

Opened this issue · 1 comments

interface TaskModel {
    Name: string;
    Type: TaskType;
    Data: TaskData;
}
type TaskData = StaticData | DynamicData | RedundancyData;
interface StaticData {
  Field: string;
}

oc(taskModel).Data.Field() // Field is not visible and checked now

I tried to use type guard, but had an error:
Error:(81, 84) TS2677: A type predicate's type must be assignable to its parameter's type. Type 'TSOCType<ChildData>' is not assignable to type 'TSOCType<TaskData>'. Type 'TSOCType<ChildData>' is not assignable to type 'TSOCDataAccessor<TaskData> & TSOCObjectWrapper<StaticData>'. Type 'TSOCType<ChildData>' is not assignable to type 'TSOCDataAccessor<TaskData>'. Types of parameters 'noDefaultValue' and 'defaultValue' are incompatible. Type 'TaskData' is not assignable to type 'undefined'. Type 'StaticData' is not assignable to type 'undefined'.

export function isChild(item: TSOCType<ITaskDTO>, val: TSOCType<TaskData>): val is TSOCType<ChildData> {
    return item.Type() !== 'Static';
}