'Dealing with strictFunctionTypes and required fields

I've run into a problem with my dynamic data tables. They're a series of configurations for each field. Everything has certain fields (id, createdAt, updatedAt, etc), but some fields are object specific.

I've reproduced this as best as I can below.

This all works as long as everything in UserData or PostData is optional. But we know letting a users email be undefined is completely pointless, in this context.

// The fields we know all objects will have
type RawData = { id: number; }

// The fields of a specific type of object
type UserData = RawData & { email: string, name: string };
type PostData = RawData & { name: string }

// A field config, for dynamic tables or similiar
type Config = {
  getValue: (data: RawData) => string;
}

const ConfigMap: Record<string, Config> = {
  // A field that is only valid for users
  USERNAME: {
    entity: ['USERS'],
    getValue: (data: UserData) => ''
  },
  // A field that is valid for either
  NAME: {
    entity: ['USERS', 'POSTS'],
    getValue: (data: UserData | PostData) => ''
  }
}

The problem is strictFunctionTypes doesn't like this. As long as everything is optional, it's fine. But the second I require something it breaks down.

Is this solvable without breaking strictFunctionTypes?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source