A
A
Arthur2022-04-05 20:16:49
typescript
Arthur, 2022-04-05 20:16:49

How to write so that an object is not equal to undefined?

const obj: {
    [ key: string ]: number
} = {
    "RUB": 83
}

const txt: {
    left: string;
} = {
    left: "RUB"
};

const qq = 1000 / obj[txt.left];


When dividing, it crashes, the object is undefined. How to properly handle such cases?

Made this workaround:
const ww = obj[txt.left] as number
const qq = 1000 / ww;

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question