I'm wondering how I can assign a value to a key that depends on another key inside that same object. In this case I want to say if animal has owner it is NOT food but if it does not have an owner it IS food. So in my example the object animal should evaluate to true and animal2 should evaluate to false but they don't.
> I have tried isFood: animal.owner ? false: true
> I have tried isFood: animal.hasOwnProperty("owner") ? false: true
> I have tried isFood: "owner" in animal ? false: true
> I have tried isFood: this.owner ? false: true
interface personInterface {
name: string,
age: number,
isMember: boolean
};
interface animalInterface<T> {
owner?: T,
sound: string,
species: string,
isFood: boolean
}
let person: personInterface;
person = {
name: "justin",
age: 30,
isMember: true
}
let animal: animalInterface<personInterface>;
let animal2: animalInterface<personInterface>;
animal = {
owner: {...person},
sound: "Woof",
species: "Dog",
isFood: this.owner ? false: true
}
animal2 = {
sound: "Woof",
species: "Dog",
isFood: this.owner ? false: true
}
CodePudding user response:
In order to achieve it, you need to use discriminated unions:
interface PersonInterface {
name: string,
age: number,
isMember: boolean
};
interface AnimalInterfaceBase<T> {
owner?: T,
sound: string,
species: string,
isFood: boolean
}
type WithOwner<T> = {
owner: T,
sound: string,
species: string,
isFood: false
}
type WithoutOwner = {
sound: string,
species: string,
isFood: true
}
type AnimalInterface<T> = WithOwner<T> | WithoutOwner
let person: PersonInterface;
person = {
name: "justin",
age: 30,
isMember: true
}
let animal: AnimalInterface<PersonInterface>;
let animal2: AnimalInterface<PersonInterface>;
animal = {
owner: person,
sound: "Woof",
species: "Dog",
isFood: false
}
animal2 = {
owner: person, // expected error
sound: "Woof",
species: "Dog",
isFood: true
}
P.S. It is conventional to capitalize interface names.
CodePudding user response:
this
may vary depending upon where it is used, inside a function or globally. In order to refer the current object and still access the key as a property, you can use get
accessor like below. You can read more about get
accessor here.
animal = {
owner: { ...person },
sound: "Woof",
species: "Dog",
get isFood() {
// Here this refers to current object
return this.owner ? false : true
}
}
animal2 = {
sound: "Woof",
species: "Dog",
get isFood() {
return this.owner ? false : true
}
}
console.log(animal.isFood);
console.log(animal2.isFood);
CodePudding user response:
You could use tagged union types in this case. Basically you create two interfaces and discern them by a property, in your case isFood
:
interface BaseAnimalInterface {
sound: string;
species: string;
isFood: boolean;
}
interface AnimalInterface extends BaseAnimalInterface {
isFood: true;
}
interface PetInterface<T = PersonInterface> extends BaseAnimalInterface {
owner: T;
isFood: false;
}
type AnimalType = AnimalInterface | PetInterface;
Then, if you check into that property on an if/switch
statement you'll get the proper type:
function playSound(animal: AnimalType): void {
if (animal.isFood) {
// Casted automatically to `AnimalInterface`
} else {
// Casted automatically to `PetInterface`
}
}