It's pretty hard to explain since I don't know all the terminology yet. But here it goes:
Given the following interface with the optional property 'id'.
interface User {
id?: string;
name: string;
}
In some parts of the application the id
property is undefined (yet). But at a certain point in my application I know for sure that the id
is there.
// at this point user.id is definitely defined
users.map((user: User) => {
return (<p>{user.name} with the id: {user.id}<p>)
// TS error: Type 'undefined' is not assignable to type 'string'.
});
Now I can add an 'if' statement to solve this issue. But is there a way that I can 'tell' Typescript that the ID property now is available for sure? Or is that not smart to do...
Ideally I do not want to have to maintain another interface or alias with the id property non-optional.