From 9275aab89958e0abe6b39752ba171a64f4908d4f Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Sun, 25 Jul 2021 06:46:42 -0700 Subject: [PATCH] [chore] prefer interfaces to types --- .changeset/curvy-dots-join.md | 5 +++ packages/kit/test/types.d.ts | 8 ++-- packages/kit/types/config.d.ts | 16 +++---- packages/kit/types/endpoint.d.ts | 4 +- packages/kit/types/hooks.d.ts | 12 +++--- packages/kit/types/internal.d.ts | 72 ++++++++++++++++---------------- packages/kit/types/page.d.ts | 18 ++++---- 7 files changed, 70 insertions(+), 65 deletions(-) create mode 100644 .changeset/curvy-dots-join.md diff --git a/.changeset/curvy-dots-join.md b/.changeset/curvy-dots-join.md new file mode 100644 index 000000000000..ee7be9d4aa62 --- /dev/null +++ b/.changeset/curvy-dots-join.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +[chore] prefer interfaces to types diff --git a/packages/kit/test/types.d.ts b/packages/kit/test/types.d.ts index cbba2445d2a0..20341218021f 100644 --- a/packages/kit/test/types.d.ts +++ b/packages/kit/test/types.d.ts @@ -7,7 +7,7 @@ import { RequestInfo, RequestInit, Response as NodeFetchResponse } from 'node-fe // seems like that's no longer an issue? in which case we don't need // to wrap all these methods -export type TestContext = { +export interface TestContext { base: string; page: Page; pages: { @@ -34,14 +34,14 @@ export type TestContext = { server: import('net').Server; reset: () => Promise; unpatch: () => void; -}; +} -type TestOptions = { +interface TestOptions { js?: boolean; nojs?: boolean; dev?: boolean; build?: boolean; -}; +} export interface TestFunctionBase { ( diff --git a/packages/kit/types/config.d.ts b/packages/kit/types/config.d.ts index 45482dddb12f..2e11ce54670d 100644 --- a/packages/kit/types/config.d.ts +++ b/packages/kit/types/config.d.ts @@ -1,7 +1,7 @@ import { Logger, TrailingSlash } from './internal'; import { UserConfig as ViteConfig } from 'vite'; -export type AdapterUtils = { +export interface AdapterUtils { log: Logger; rimraf: (dir: string) => void; mkdirp: (dir: string) => void; @@ -18,14 +18,14 @@ export type AdapterUtils = { dest: string; fallback?: string; }) => Promise; -}; +} -export type Adapter = { +export interface Adapter { name: string; adapt: ({ utils, config }: { utils: AdapterUtils; config: ValidatedConfig }) => Promise; -}; +} -export type Config = { +export interface Config { compilerOptions?: any; extensions?: string[]; kit?: { @@ -76,9 +76,9 @@ export type Config = { vite?: ViteConfig | (() => ViteConfig); }; preprocess?: any; -}; +} -export type ValidatedConfig = { +export interface ValidatedConfig { compilerOptions: any; extensions: string[]; kit: { @@ -130,4 +130,4 @@ export type ValidatedConfig = { vite: () => ViteConfig; }; preprocess: any; -}; +} diff --git a/packages/kit/types/endpoint.d.ts b/packages/kit/types/endpoint.d.ts index 2687562aebf6..1b4df7f6aed5 100644 --- a/packages/kit/types/endpoint.d.ts +++ b/packages/kit/types/endpoint.d.ts @@ -12,11 +12,11 @@ type JSONValue = type DefaultBody = JSONValue | Uint8Array; -export type EndpointOutput = { +export interface EndpointOutput { status?: number; headers?: Headers; body?: Body; -}; +} export type RequestHandler< Locals = Record, diff --git a/packages/kit/types/hooks.d.ts b/packages/kit/types/hooks.d.ts index d50776a133f7..c80787f58a2d 100644 --- a/packages/kit/types/hooks.d.ts +++ b/packages/kit/types/hooks.d.ts @@ -2,23 +2,23 @@ import { Headers, Location, MaybePromise, ParameterizedBody } from './helper'; export type StrictBody = string | Uint8Array; -export type ServerRequest, Body = unknown> = Location & { +export interface ServerRequest, Body = unknown> extends Location { method: string; headers: Headers; rawBody: StrictBody; body: ParameterizedBody; locals: Locals; -}; +} -export type ServerResponse = { +export interface ServerResponse { status: number; headers: Headers; body?: StrictBody; -}; +} -export type GetSession, Session = any> = { +export interface GetSession, Session = any> { (request: ServerRequest): MaybePromise; -}; +} export type Handle> = (input: { request: ServerRequest; diff --git a/packages/kit/types/internal.d.ts b/packages/kit/types/internal.d.ts index 599c481ae1fc..b76913cf2369 100644 --- a/packages/kit/types/internal.d.ts +++ b/packages/kit/types/internal.d.ts @@ -5,23 +5,23 @@ import { Load } from './page'; type PageId = string; -export type Incoming = Omit & { +export interface Incoming extends Omit { method: string; headers: Headers; rawBody: StrictBody; body?: ParameterizedBody; -}; +} -export type Logger = { +export interface Logger { (msg: string): void; success: (msg: string) => void; error: (msg: string) => void; warn: (msg: string) => void; minor: (msg: string) => void; info: (msg: string) => void; -}; +} -export type App = { +export interface App { init: ({ paths, prerendering, @@ -44,9 +44,9 @@ export type App = { }; } ) => Promise; -}; +} -export type SSRComponent = { +export interface SSRComponent { ssr?: boolean; router?: boolean; hydrate?: boolean; @@ -65,7 +65,7 @@ export type SSRComponent = { }; }; }; -}; +} export type SSRComponentLoader = () => Promise; @@ -73,14 +73,14 @@ export type CSRComponent = any; // TODO export type CSRComponentLoader = () => Promise; -export type SSRPagePart = { +export interface SSRPagePart { id: string; load: SSRComponentLoader; -}; +} export type GetParams = (match: RegExpExecArray) => Record; -export type SSRPage = { +export interface SSRPage { type: 'page'; pattern: RegExp; params: GetParams; @@ -90,16 +90,16 @@ export type SSRPage = { // plan b — and render that instead a: PageId[]; b: PageId[]; -}; +} -export type SSREndpoint = { +export interface SSREndpoint { type: 'endpoint'; pattern: RegExp; params: GetParams; load: () => Promise<{ [method: string]: RequestHandler; }>; -}; +} export type SSRRoute = SSREndpoint | SSRPage; @@ -109,28 +109,28 @@ export type CSREndpoint = [RegExp]; export type CSRRoute = CSREndpoint | CSRPage; -export type SSRManifest = { +export interface SSRManifest { assets: Asset[]; layout: string; error: string; routes: SSRRoute[]; -}; +} -export type Hooks = { +export interface Hooks { getSession: GetSession; handle: Handle; serverFetch: ServerFetch; -}; +} -export type SSRNode = { +export interface SSRNode { module: SSRComponent; entry: string; // client-side module corresponding to this component css: string[]; js: string[]; styles: string[]; -}; +} -export type SSRRenderOptions = { +export interface SSRRenderOptions { amp: boolean; dev: boolean; entry: { @@ -157,9 +157,9 @@ export type SSRRenderOptions = { target: string; template: ({ head, body }: { head: string; body: string }) => string; trailing_slash: TrailingSlash; -}; +} -export type SSRRenderState = { +export interface SSRRenderState { fetched?: string; initiator?: SSRPage | null; prerender?: { @@ -169,54 +169,54 @@ export type SSRRenderState = { error: Error; }; fallback?: string; -}; +} -export type Asset = { +export interface Asset { file: string; size: number; type: string | null; -}; +} -export type PageData = { +export interface PageData { type: 'page'; pattern: RegExp; params: string[]; path: string; a: string[]; b: string[]; -}; +} -export type EndpointData = { +export interface EndpointData { type: 'endpoint'; pattern: RegExp; params: string[]; file: string; -}; +} export type RouteData = PageData | EndpointData; -export type ManifestData = { +export interface ManifestData { assets: Asset[]; layout: string; error: string; components: string[]; routes: RouteData[]; -}; +} -export type BuildData = { +export interface BuildData { client: string[]; server: string[]; static: string[]; entries: string[]; -}; +} -export type NormalizedLoadOutput = { +export interface NormalizedLoadOutput { status: number; error?: Error; redirect?: string; props?: Record | Promise>; context?: Record; maxage?: number; -}; +} export type TrailingSlash = 'never' | 'always' | 'ignore'; diff --git a/packages/kit/types/page.d.ts b/packages/kit/types/page.d.ts index 8bdc9888feee..05a06ae8c73e 100644 --- a/packages/kit/types/page.d.ts +++ b/packages/kit/types/page.d.ts @@ -1,36 +1,36 @@ import { Location as Page, MaybePromise, InferValue } from './helper'; -export type LoadInput< +export interface LoadInput< PageParams extends Record = Record, Context extends Record = Record, Session = any -> = { +> { page: Page; fetch: (info: RequestInfo, init?: RequestInit) => Promise; session: Session; context: Context; -}; +} -export type ErrorLoadInput< +export interface ErrorLoadInput< PageParams extends Record = Record, Context extends Record = Record, Session = any -> = LoadInput & { +> extends LoadInput { status?: number; error?: Error; -}; +} -export type LoadOutput< +export interface LoadOutput< Props extends Record = Record, Context extends Record = Record -> = { +> { status?: number; error?: string | Error; redirect?: string; props?: Props; context?: Context; maxage?: number; -}; +} // Publicized Types export type Load<