Как понизить в Typescript?

Как мне понизить в машинописном тексте?

const x: {a: number, b: number} = {a: 1, b: 2};

const y: {b: number}            = x;   //   upcast
const z: {a: number, b: number} = ???; // downcast

Я могу преобразовать x в y, но что нужно добавить в ???, чтобы преобразовать y в z?


Для сравнения, C# будет выглядеть так

AandB x = ...
A y = x;
B z = y as B;

person Paul Draper    schedule 06.07.2017    source источник
comment
y as { a: number, b: number } должно работать   -  person artem    schedule 07.07.2017


Ответы (1)


Попробуйте что-то вроде:

type AandB = { a: number, b: number };

const x: AandB = { a: 1, b: 2 };
const y: {b: number} = x; // upcast
const z: { a: number, b: number } = y as AandB; // downcast
person tony    schedule 07.07.2017