Home > database >  Typescript rename type
Typescript rename type

Time:02-04

how can I get vscode to suggest the key of mainMap, as socketId: string instead of only string?

type SocketId = string;
type User = {displayName: string}
let mainMap = new Map<SocketId, User>();

CodePudding user response:

You can add an intersection with {}. This will not have any impact on assignability, but it will prevent TS form inlining the alias:

type SocketId = string & {};
type User = {displayName: string}
let mainMap = new Map<SocketId, User>();

Playground Link

You might also consider branded types to check that the socket id is valid.

  •  Tags:  
  • Related