You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Search Terms: generic type constraint undefined enum
Code
enumMyEvent{Start='start',Stop='stop',}interfaceMyEventOptions{[MyEvent.Start]: {startTime: number},[MyEvent.Stop]: {endTime: number},}classEventService{privateeventCounts: {[keyinMyEvent]?: number};constructor(){// I don't want to have to list every possible enum member herethis.eventCounts={};}privatestartEvent(){this.report(MyEvent.Start,{startTime: 0});// This should be invalid:// // this.report(MyEvent.Start, { endTime: 0 });}privatereport<TextendskeyofMyEventOptions>(event: T,options: MyEventOptions[T]){// errors with `Type 'undefined' is not assignable to type 'number'.`constcount: number=this.eventCounts[event]||0;// This would do, for example://// request(mixpanelUrl, { name: event, options });}}
It doesn't complain if you change it to const count: number = (this.eventCounts.get(event) as number | undefined) || 0;
Expected behavior: Compiles without a problem.
Actual behavior:
Type '{ start?: number | undefined; stop?: number | undefined; }[T]' is not assignable to type 'number'.
Type 'number | undefined' is not assignable to type 'number'.
Type 'undefined' is not assignable to type 'number'.
TypeScript Version: 3.5.2
Search Terms: generic type constraint undefined enum
Code
It doesn't complain if you change it to
const count: number = (this.eventCounts.get(event) as number | undefined) || 0;
Expected behavior: Compiles without a problem.
Actual behavior:
Playground Link: https://is.gd/S57reZ
Related Issues: #32017
The text was updated successfully, but these errors were encountered: