Skip to content

Instantly share code, notes, and snippets.

@ryanpcmcquen
Last active July 14, 2023 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanpcmcquen/9264998770139c88d58d9e557c2f44eb to your computer and use it in GitHub Desktop.
Save ryanpcmcquen/9264998770139c88d58d9e557c2f44eb to your computer and use it in GitHub Desktop.
TypeScript

Remapping types:

You can alter type keys with expressions:

export type PrefixKeyWithColon<Type> = {
    [Prop in keyof Type & string as `:${Prop}`]: Type[Prop]
}

And then apply that to a new type:

export type FooApiExpressionAttributeValues = Partial<PrefixKeyWithColon<FooApi>>

And with FooApi equaling this:

export interface FooApi {
    id: string
    createdAt: string
    updatedAt: string
    name: string
    slug: string
    description?: string
}

FooApiExpressionAttributeValues would compute to:

type FooApiExpressionAttributeValues {
    ":id": string | undefined
    ":createdAt": string | undefined
    ":updatedAt": string | undefined
    ":name": string | undefined
    ":slug": string | undefined
    ":description"?: string | undefined
}

Destructured object parameters:

const createMessageBodyFromIntermediates = async ({
    newProfile,
    oldProfile,
    messageType,
    actionBy = 'manual-network-call',
    tracingId,
    isCorrection,
    shadowMode = false,
}: {
    newProfile: ProfileIntermediate
    oldProfile: ProfileIntermediate | null
    messageType: string
    actionBy: string
    tracingId?: string
    isCorrection?: boolean
    shadowMode?: boolean
}): Promise<kafkaMessage> => {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment