-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Closed as not planned
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed
Description
π Search Terms
object literal type inference
enum type narrowing
π Version & Regression Information
- This changed between versions 5.0.4 and 5.1.6
β― Playground Link
π» Code
enum Events {
EVENT1 = 'Event 1',
EVENT2 = 'Event 2',
}
interface GenericEvent {
eventName: Events;
properties?: never;
}
interface Event1Event {
eventName: Events.EVENT1;
properties: {
data: string
};
}
interface Event2Event {
eventName: Events.EVENT2;
properties: {
data: string
};
}
type EventTypes =
| GenericEvent
| Event1Event
| Event2Event
const num = Math.floor(Math.random() * 10) + 1;
const eventName = num > 5 ? Events.EVENT1 : Events.EVENT2;
// This version fails on properties: Type '{ data: string; }' is not assignable to type 'undefined'
processEvent({
eventName,
properties: {
data: "My Event Data"
}
});
// This version works
const data = {
eventName,
properties: {
data: "My Event Data"
}
};
processEvent(data);
function processEvent(event: EventTypes) {
console.log(event);
}
π Actual behavior
The event passed to processEvent
behaves differently if assigned to a const prior to being passed in.
π Expected behavior
Both should be the same result. It's also worth noting that in the repro provided assignment to a const data = ...
works to resolve the problem. But in my code this doesn't end up working and instead I need to explicitly state that eventName
can only be one of the two Events
values in the ternary.
Additional information about the issue
No response
Metadata
Metadata
Assignees
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed