-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Type not assignable for string literal in signature #38201
Comments
Another reproduction link: https://codesandbox.io/s/laughing-shockley-hlpr1?file=/src/index.ts |
This clearly shouldn't work given that ({} as KeyStore).generate("EC", 128) and shouldn't match any overload. |
@DanielRosenwasser Correct, but that is not what the error message is suggesting. interface KeyStore {
generate(kty: 'EC', crv?: string, parameters?: object, isprivate?: boolean): void;
generate(kty: 'OKP', crv?: string, parameters?: object, isprivate?: boolean): void;
generate(kty: 'RSA', bitlength?: number, parameters?: object, isprivate?: boolean): void;
generate(kty: 'oct', bitlength?: number, parameters?: object): void;
}
type tAlgorithm = "RSA" | "EC" | "OKP" | "oct";
function generateKey(alg: tAlgorithm) {
return ({} as KeyStore).generate(alg);
} This still gives the error. Also, could you provide an alternative approach for this function if I want to pass string literals as a parameter of the function? |
However, I think that even with the ideas of #30799 incorporated, it wouldn't help you write the original code easily without a type assertion or duplicate overloads. |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
TypeScript Version: 3.8.3
Search Terms:
string literal
string alias
is not assignable to parameter of type
Expected behavior:
No type check errors
Actual behavior:
Related Issues:
Code
Output
Compiler Options
Playground Link: Provided
The text was updated successfully, but these errors were encountered: