Search Terms
Function, String Literal
Suggestion
Show code:
interface T {
(name: 'a', options: { key1: string }): string
(name: 'b', options: { key2: string }): string
(name: string, options?: any): string
}
const t: T = name => name
t('a')
// ^^^^ no error occurs
I know why there is no error. Typescript try to match the first overload. But no match. Then try to match second overload. Also not. But matched in the last overload. So there is no errors occur.
But I suggest typescript to throw error. I want function invoked with 'a' must have the second param {key1: string}, and invoked with 'b' must have the second param {key2: string}. The other string without any limited.
Use Cases
I write a i18n function to generate string. In some key, the second param cannot be ignored. And others is no limited.
function intl(key: 'X', value: {key: string}): string
function intl(key: string, value: any): string
intl('X') // error
intl('X', {key: 'aaa'}) // correct
Examples
In above.
Checklist
My suggestion meets these guidelines:
Search Terms
Function, String Literal
Suggestion
Show code:
I know why there is no error. Typescript try to match the first overload. But no match. Then try to match second overload. Also not. But matched in the last overload. So there is no errors occur.
But I suggest typescript to throw error. I want function invoked with 'a' must have the second param
{key1: string}, and invoked with 'b' must have the second param{key2: string}. The other string without any limited.Use Cases
I write a i18n function to generate string. In some
key, the second param cannot be ignored. And others is no limited.Examples
In above.
Checklist
My suggestion meets these guidelines: