Consider a scenario where I have a structure that defines a user along with their login and given name:
export interface User {
login: string;
name: string;
}
Now, my objective is to make an API call using the user's login information:
const formatLogin = (login: string) => login.toLowerCase().trim();
const fetchUser = (login: string) => ajax(`${API}/users/${formatLogin(login)}`);
Initially, calling getUser(this.user.name)
seemed correct. However, this leads to an error, as it should be this.user.login
. TypeScript doesn't raise any warnings due to them both being strings.
Is there a method in TypeScript to ensure that a specific property is utilized?
I attempted defining export type Username = string
, but it was ineffectual since the name
property still matches the criteria of a simple string
. Similarly, creating an export interface Username {}
failed to function correctly with the formatLogin
function requiring string methods like toLowerCase
and trim
. Trying
interface Username extends String {}
presented the same issue.