Skip to content

Commit 215d9ea

Browse files
authored
refactor: simplify title types (#8)
* refactor: simplify title types * Add large-ads-kiss.md
1 parent de305fd commit 215d9ea

5 files changed

Lines changed: 18 additions & 20 deletions

File tree

.changeset/large-ads-kiss.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tanstack-meta": patch
3+
---
4+
5+
refactor: simplify title types

packages/meta/src/normalize/opengraph.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { InputMetadata, SimplifyTitleInUnion } from "../types/io";
1+
import type { InputMetadata } from "../types/io";
22
import type {
33
ResolvedMetadata,
44
ResolvedMetadataWithURLs,
@@ -125,9 +125,7 @@ function getFieldsByOgType(ogType: OpenGraphType | undefined) {
125125
export function normalizeOpenGraph(openGraph: InputMetadata["openGraph"]) {
126126
if (!openGraph) return null;
127127

128-
type OpenGraphResolved = NonNullable<
129-
SimplifyTitleInUnion<ResolvedMetadata["openGraph"]>
130-
>;
128+
type OpenGraphResolved = NonNullable<ResolvedMetadata["openGraph"]>;
131129
type OpenGraphArrayKeys =
132130
(typeof OgTypeFields)[keyof typeof OgTypeFields][number];
133131

@@ -189,7 +187,7 @@ export function normalizeTwitter(twitter: InputMetadata["twitter"]) {
189187
const resolved = {
190188
...twitter,
191189
title: twitter.title,
192-
} as NonNullable<SimplifyTitleInUnion<ResolvedMetadata["twitter"]>>;
190+
} as NonNullable<ResolvedMetadata["twitter"]>;
193191
for (const infoKey of TwitterBasicInfoKeys) {
194192
resolved[infoKey] = twitter[infoKey] || null;
195193
}

packages/meta/src/types/io.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ import type {
66
ResolvedMetadataWithURLs,
77
} from "./metadata-interface";
88

9-
// Simplify the type of the title property in the opengraph type
10-
export type SimplifyTitleInUnion<T> = T extends { title?: unknown }
11-
? Omit<T, "title"> & { title?: string }
12-
: T;
13-
149
export type InputMetadata = {
1510
/**
1611
* The character set of the document.
@@ -358,7 +353,7 @@ export type InputMetadata = {
358353
*
359354
* @see https://ogp.me/
360355
*/
361-
openGraph?: SimplifyTitleInUnion<NextMetadata["openGraph"]>;
356+
openGraph?: NextMetadata["openGraph"];
362357
/**
363358
* The Twitter metadata for the document.
364359
*
@@ -381,7 +376,7 @@ export type InputMetadata = {
381376
* // <meta name="twitter:image" content="https://example.com/og.png" />
382377
* ```
383378
*/
384-
twitter?: SimplifyTitleInUnion<NextMetadata["twitter"]>;
379+
twitter?: NextMetadata["twitter"];
385380
/**
386381
* The Facebook AppLinks metadata for the document.
387382
*
@@ -453,8 +448,8 @@ export type NormalizedMetadata = {
453448
formatDetection: ResolvedMetadata["formatDetection"];
454449
verification: ResolvedMetadata["verification"];
455450
appleWebApp: ResolvedMetadata["appleWebApp"];
456-
openGraph: SimplifyTitleInUnion<ResolvedMetadata["openGraph"]>;
457-
twitter: SimplifyTitleInUnion<ResolvedMetadata["twitter"]>;
451+
openGraph: ResolvedMetadata["openGraph"];
452+
twitter: ResolvedMetadata["twitter"];
458453
appLinks: ResolvedMetadataWithURLs["appLinks"];
459454
icons: ResolvedMetadata["icons"];
460455
};

packages/meta/src/types/opengraph-types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// https://github.com/vercel/next.js/blob/f9f625b90e6d4a562758c6a43234e168dcc23aa1/packages/next/src/lib/metadata/types/opengraph-types.ts
22

3-
import type { AbsoluteTemplateString, TemplateString } from "./metadata-types";
3+
import type { AbsoluteTemplateString } from "./metadata-types";
44

55
export type OpenGraphType =
66
| "article"
@@ -36,7 +36,7 @@ type Locale = string;
3636

3737
type OpenGraphMetadata = {
3838
determiner?: "a" | "an" | "the" | "auto" | "" | undefined;
39-
title?: string | TemplateString | undefined;
39+
title?: string | undefined;
4040
description?: string | undefined;
4141
emails?: string | Array<string> | undefined;
4242
phoneNumbers?: string | Array<string> | undefined;
@@ -195,7 +195,7 @@ export type ResolvedOpenGraph =
195195

196196
type ResolvedOpenGraphMetadata = {
197197
determiner?: "a" | "an" | "the" | "auto" | "" | undefined;
198-
title: AbsoluteTemplateString;
198+
title?: string | undefined;
199199
description?: string | undefined;
200200
emails?: Array<string> | undefined;
201201
phoneNumbers?: Array<string> | undefined;

packages/meta/src/types/twitter-types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// Reference: https://developer.twitter.com/en/docs/twitter-for-websites/cards/overview/markup
44

5-
import type { AbsoluteTemplateString, TemplateString } from "./metadata-types";
5+
import type { AbsoluteTemplateString } from "./metadata-types";
66

77
export type Twitter =
88
| TwitterSummary
@@ -18,7 +18,7 @@ type TwitterMetadata = {
1818
creator?: string | undefined; // username for the account associated to the creator of the content on the site
1919
creatorId?: string | undefined; // id for the account associated to the creator of the content on the site
2020
description?: string | undefined;
21-
title?: string | TemplateString | undefined;
21+
title?: string | undefined;
2222
images?: TwitterImage | Array<TwitterImage> | undefined;
2323
};
2424
type TwitterSummary = TwitterMetadata & {
@@ -81,7 +81,7 @@ type ResolvedTwitterSummary = {
8181
creator: string | null;
8282
creatorId: string | null;
8383
description: string | null;
84-
title: AbsoluteTemplateString;
84+
title?: string | undefined;
8585
images?: Array<ResolvedTwitterImage> | undefined;
8686
};
8787
type ResolvedTwitterPlayer = ResolvedTwitterSummary & {

0 commit comments

Comments
 (0)