Attribution does not support Array

The terms of service for some APIs require you to provide visual attribution when you display their data. This can be accommodated in Packs using the attribution field of the schema. You can include a mix of text, links, and images which will be displayed when the user hovers over the object’s chip.

let TaskSchema = coda.makeObjectSchema({
  // ...
  attribution: [
    {
      type: coda.AttributionNodeType.Text,
      text: "Provided by Todoist",
    },
    {
      type: coda.AttributionNodeType.Link,
      anchorText: "todoist.com",
      anchorUrl: "https://todoist.com",
    },
    {
      type: coda.AttributionNodeType.Image,
      imageUrl: "https://todoist.com/favicon.ico",
      anchorUrl: "https://todoist.com",
    },
  ],
});

However, the typescript type does not support arrays:

export declare type AttributionNode = TextAttributionNode | LinkAttributionNode | ImageAttributionNode;

Resulting in this error:

Type '({ type: AttributionNodeType; text: string; anchorText?: undefined; anchorUrl?: undefined; imageUrl?: undefined; } | { type: AttributionNodeType; anchorText: string; anchorUrl: string; text?: undefined; imageUrl?: undefined; } | { ...; })[]' is not assignable to type 'AttributionNode[]'.
  Type '{ type: AttributionNodeType; text: string; anchorText?: undefined; anchorUrl?: undefined; imageUrl?: undefined; } | { type: AttributionNodeType; anchorText: string; anchorUrl: string; text?: undefined; imageUrl?: undefined; } | { ...; }' is not assignable to type 'AttributionNode'.
    Type '{ type: coda.AttributionNodeType; text: string; anchorText?: undefined; anchorUrl?: undefined; imageUrl?: undefined; }' is not assignable to type 'AttributionNode'.
      Type '{ type: coda.AttributionNodeType; text: string; anchorText?: undefined; anchorUrl?: undefined; imageUrl?: undefined; }' is not assignable to type 'ImageAttributionNode'.
        Types of property 'type' are incompatible.
          Type 'AttributionNodeType' is not assignable to type 'AttributionNodeType.Image'.ts(2322)
schema.d.ts(725, 5): The expected type comes from property 'attribution' which is declared here on type 'Omit<ObjectSchemaDefinition<string, string>, "type"> & { type?: ValueType.Object; }'

With sample code:


const Attribution = [
	{
		type: coda.AttributionNodeType.Text,
		text: 'Provided by CoinGecko',
	},
	{
		type: coda.AttributionNodeType.Link,
		anchorText: 'coingecko.com',
		anchorUrl: 'https://coingecko.com',
	},
	{
		type: coda.AttributionNodeType.Image,
		imageUrl: 'https://coingecko.com/favicon.ico',
		anchorUrl: 'https://coingecko.com',
	},
]
const CoinsSchema = coda.makeObjectSchema({
	resultType: coda.ValueType.Array,
	items: CoinSchema,
	featuredProperties: ['id', 'symbol', 'name'],
	attribution: Attribution,
})

Looks like you need to explicitly declareAttributionNode[] type to the Attribution variable to eliminate the typescript error.

const Attribution: AttributionNode[] = [
	{
		type: coda.AttributionNodeType.Text,
		text: 'Provided by CoinGecko',
	},
	{
		type: coda.AttributionNodeType.Link,
		anchorText: 'coingecko.com',
		anchorUrl: 'https://coingecko.com',
	},
	{
		type: coda.AttributionNodeType.Image,
		imageUrl: 'https://coingecko.com/favicon.ico',
		anchorUrl: 'https://coingecko.com',
	},
]

I don’t know why it works tbh. I know when you pass the array inline in the object you don’t get the error.

2 Likes

CHAMPION

Note that it also requires:

import type { AttributionNode } from '@codahq/packs-sdk'

Thanks buddy

Alternatively, just use coda.AttributionNode, since the whole SDK is already imported as coda.