Closed
Description
Bug Report
Compiler shouldn't allow number
to be used where enum
is expected.
🔎 Search Terms
number where enum expected
🕗 Version & Regression Information
- This is a crash
- This changed between versions ______ and _______
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about enums_and_function_params
- I was unable to test this on prior versions because _______
⏯ Playground Link
Playground link with relevant code
💻 Code
export enum StatusEnum {
Cancelled = 4,
Paid = 6
}
export interface Summary {
readonly status: number;
}
export class TripSummary {
private readonly status: StatusEnum;
public constructor(dto: Summary, status: number) {
this.status = status; // status is number, but allowed to be assigned to an enum field
this.setFullStatus(dto.status); // dto.status is number, but allowed to be passed as an enum param
}
private setFullStatus(status: StatusEnum): void {
console.log(status);
}
}
🙁 Actual behavior
Compiler allows number
to be used where enum
is expected. It's incorrect because narrowing conversion is done implicitly while the compiler cannot make sure that a value passed as the enum param is within a range of the enum values.
🙂 Expected behavior
Implicit conversions from number
to enum
should generate an error requiring an explicit conversion to be performed (for exampe, using as
)