Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking β€œSign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
taozhou-glean opened this issue Jan 23, 2025 Β· 2 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@taozhou-glean
Copy link

taozhou-glean commented Jan 23, 2025 β€’

πŸ”Ž 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

@RyanCavanaugh
Copy link
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.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jan 23, 2025
@taozhou-glean
Copy link
Author

i see, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

2 participants