My question is pretty simple here, I would like to understand the the relationship between casting a literal with as const
and the infer of a generics.
Here a sample repro:
declare global {
interface String {
// @ts-expect-error Simulate skipLibCheck
toUpperCase<T>(this: T): Uppercase<T>;
}
}
export {};
const strA = "mylowercasedstring"; // typed as "mylowercasedstring" (literal)
const strB = "mylowercasedstring" as const; // also typed as "mylowercasedstring" (literal)
const resultA = strA.toUpperCase(); // typed as string
const resultB = strB.toUpperCase(); // typed as "MYLOWERCASEDSTRING" (literal)
// why?
Why resultA
is not properly infered like resultB
?