We ran into the issue that a property was undefined even though the type doesn't allow it. Turns out the way we create the object in place has some issues. I made a small example which the linter accepts for some weird reason. I know how this can be fixed or should be used but want to be sure that there are and will be no traps like this in the future. It allows joeFriend.kidName to be undefined in spite of the cast to a type which doesn't allow it. If I remove the cast it knows that it can be undefined. If I remove "kidName" from that object it also allows it to be cast and created if I explicitly say that kidName:undefined it shows an error.
type person = {
name: string;
kid: { name: string; age: number } | undefined;
};
type friend = {
name: string;
kidName: string;
};
const joe = {
name: "Joe",
kid: undefined,
} as person;
const joeFriend = {
name: joe.name,
kidName: joe.kid?.name,
} as friend;
edit:
please disregard what I call cast or linter, you all know what I'm talking about. If this is just "compiler thinks you know better", why doesn't it allow this?
const joeFriend = {
name: joe.name,
kidName: undefined,
} as Friend
any other language wouldn't allow this, all I asked is how would I be able to have strict check on this, nothing else
unknown, sox as unknown as Twill even allow sidecasting, since it is an upcast followed by a downcast. Does that fully address the question now? If so I'll answer (or more likely close as a duplicate). If not, please edit to clarify.