Skip to content

type restriction no longer respected with indirect call involving const enum with number values #61033

Closed
@taozhou-glean

Description

@taozhou-glean

πŸ”Ž Search Terms

const enum

πŸ•— Version & Regression Information

not sure, i think it has been like this for a long time, still present in latest version 5.7.2

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.7.3#code/KYDwDg9gTgLgBAYwgOwM72MgrgWzgFWHTgG8AoOOAQTgF44BmABiYBoK4AhOuARgCYGZAL5kySNPBhEYAMWQ8AFAEMAXARkBKOgD44ysdPTzFhdADoqmsQHobcYFCjR9qB+GAJpAEzJG5yIr81uIoxP4AksjeAJZQnjAAwsoANilKanDYOABGjtq0ev4mytp2cDHwqAAWEFgp3g5OLtWOwK5wAO7AaWJk5SmVjqlwMACeYMBkoJCwoxPtZjAAMkNQqfgLPMxMcAA+fIKhkqMy8vwZ6kur0uspm5MFegZ+Z8j8pjKWIeWOzlAdGYJYC+YrvIIhCThGRRWLxLzJNIXegqdTZPJQJ6nYzg0qUX7NAHKNxArwgshAA

πŸ’» Code

export const enum Test {
  A = 300,
  B = 123
}

const testFn = (a: Test) => a

testFn(Test.A)

// error as expected
testFn(2)

const testIndirectCall = (a: number) => testFn(a) // it should error here as well


// literal type
export type TestLiteralType = 300 | 123
const testFn2 = (a: TestLiteralType) => a

testFn2(Test.A)

// error as expected
testFn2(2)

const testIndirectCall2 = (a: number) => testFn2(a)  // error as expected

πŸ™ Actual behavior

as noted in above

πŸ™‚ Expected behavior

as noted in above

Additional information about the issue

No response

Activity

RyanCavanaugh

RyanCavanaugh commented on Jan 23, 2025

@RyanCavanaugh
Member

number assignable to enum is intentional; see #26362 (comment) and others. When the number is literal and we can see it's out-of-range, that's also an intentional error. The difference in behavior in different inputs is therefore expected.

taozhou-glean

taozhou-glean commented on Jan 23, 2025

@taozhou-glean
Author

i see, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @RyanCavanaugh@taozhou-glean

        Issue actions

          type restriction no longer respected with indirect call involving const enum with number values Β· Issue #61033 Β· microsoft/TypeScript