Я создал универсальный c интерфейс изображения, затем, когда мне нужно использовать некоторые из этих значений в другом универсальном c интерфейсе с использованием того же параметра универсального c, Typescript, похоже, не может вывести печатает правильно. Кажется, что Typescript пытается взять каждый вариант с "|" оператор.
Я что-то не так делаю?
type ImageType = "require" | "uri";
interface Common<T extends ImageType> {
type: T;
other?: string;
stuff?: string;
}
interface Require extends Common<"require"> {
source: number;
mime: string;
}
interface URI extends Common<"uri"> {
source: string;
mime?: string;
}
type Image<T extends ImageType> =
T extends "require" ? Require
: T extends "uri" ? URI
: Common<T>;
interface Custom<T extends ImageType> {
type: Image<T>["type"];
source?: Image<T>["source"];
mime: Image<T>["mime"];
other?: string;
stuff?: string;
}
const example0 = (image: Image<ImageType>) => "do something";
const exemple1 = (custom: Custom<ImageType>) => custom.source &&
example0({type: custom.type, source: custom.source, mime: custom.mime})
Который производится в последней строке: (У меня есть добровольно типы Custom<ImageType>
& Image<ImageType>
, потому что я хочу любой тип изображения в этом примере)
Argument of type '{ type: ImageType; source: string | number; mime: string | undefined; }' is not assignable to parameter of type 'Require | URI'.
Type '{ type: ImageType; source: string | number; mime: string | undefined; }' is not assignable to type 'URI'.
Types of property 'source' are incompatible.
Type 'string | number' is not assignable to type 'string'.
Type 'number' is not assignable to type 'string'.