Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/fable-library-ts/Reflection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ export function getEnumName(t: TypeInfo, v: number): string {
return getEnumCase(t, v)[0];
}

export function isEnumDefined(t: TypeInfo, v: string | number): boolean {
export function isEnumDefined(t: TypeInfo, v: any): boolean {
try {
const kv = getEnumCase(t, v);
return kv[0] != null && kv[0] !== "";
Expand Down
8 changes: 8 additions & 0 deletions tests/Js/Main/EnumTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ module EnumOperations =
// must be carried to TypeScript as `<'TEnum extends number>` (otherwise TS2362)
let enumToInt<'TEnum when 'TEnum: enum<int>> (value: 'TEnum) : int =
int (LanguagePrimitives.EnumToValue value)

// Unconstrained 'a passed to Enum.IsDefined; the TS output must accept any value (otherwise TS2345)
let isDefinedGeneric<'a> (t: System.Type) (value: 'a) : bool = System.Enum.IsDefined(t, value)
// let enumOfValue3 value = LanguagePrimitives.EnumOfValue value
// let enumOfValue4 = LanguagePrimitives.EnumOfValue

Expand Down Expand Up @@ -258,6 +261,11 @@ let tests =
Enum.IsDefined(t, 5y) |> equal true
Enum.IsDefined(t, 10y) |> equal false

testCase "Enum.IsDefined works in a generic function" <| fun () ->
EnumOperations.isDefinedGeneric typeof<MyEnum> "Foo" |> equal true
EnumOperations.isDefinedGeneric typeof<MyEnum> 5y |> equal true
EnumOperations.isDefinedGeneric typeof<MyEnum> 10y |> equal false

testCase "Enum.Parse works" <| fun () ->
let t = typeof<MyEnum>
Enum.Parse(t, "Foo") |> equal (box MyEnum.Foo)
Expand Down
Loading