Skip to content

Commit

Permalink
refactor: add some missing explicit types (denoland#3997)
Browse files Browse the repository at this point in the history
* refactor: add some missing return types and mark some non-exported types as `@internal`

* Update expect/fn.ts

Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>

* Remove @Internals

* nits

---------

Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
  • Loading branch information
dsherret and iuioiua committed Dec 19, 2023
1 parent 83fe26f commit b080361
Show file tree
Hide file tree
Showing 38 changed files with 146 additions and 118 deletions.
2 changes: 1 addition & 1 deletion csv/csv_parse_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export class CsvParseStream<
}
}

get readable() {
get readable(): ReadableStream<RowType<T>> {
return this.#readable as ReadableStream<RowType<T>>;
}

Expand Down
2 changes: 1 addition & 1 deletion csv/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from "./_io.ts";
import { assert } from "../assert/assert.ts";

export { ParseError, ReadOptions };
export { ParseError, type ParseResult, ReadOptions };

const BYTE_ORDER_MARK = "\ufeff";

Expand Down
2 changes: 1 addition & 1 deletion data_structures/binary_heap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class BinaryHeap<T> implements Iterable<T> {
#data: T[] = [];
constructor(private compare: (a: T, b: T) => number = descend) {}
/** Returns the underlying cloned array in arbitrary order without sorting */
toArray() {
toArray(): T[] {
return Array.from(this.#data);
}
/** Creates a new binary heap from an array like or iterable object. */
Expand Down
4 changes: 2 additions & 2 deletions data_structures/comparators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
/** This module is browser compatible. */

/** Compares its two arguments for ascending order using JavaScript's built in comparison operators. */
export function ascend<T>(a: T, b: T) {
export function ascend<T>(a: T, b: T): -1 | 0 | 1 {
return a < b ? -1 : a > b ? 1 : 0;
}

/** Compares its two arguments for descending order using JavaScript's built in comparison operators. */
export function descend<T>(a: T, b: T) {
export function descend<T>(a: T, b: T): -1 | 0 | 1 {
return a < b ? 1 : a > b ? -1 : 0;
}
8 changes: 4 additions & 4 deletions datetime/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const SECOND = 1e3;
* console.log(MINUTE); // => 60000 (60 * 1000)
* ```
*/
export const MINUTE = SECOND * 60;
export const MINUTE: number = SECOND * 60;
/**
* The number of milliseconds in an hour.
*
Expand All @@ -33,7 +33,7 @@ export const MINUTE = SECOND * 60;
* console.log(HOUR); // => 3600000 (60 * 60 * 1000)
* ```
*/
export const HOUR = MINUTE * 60;
export const HOUR: number = MINUTE * 60;
/**
* The number of milliseconds in a day.
*
Expand All @@ -44,7 +44,7 @@ export const HOUR = MINUTE * 60;
* console.log(DAY); // => 86400000 (24 * 60 * 60 * 1000)
* ```
*/
export const DAY = HOUR * 24;
export const DAY: number = HOUR * 24;
/**
* The number of milliseconds in a week.
*
Expand All @@ -55,4 +55,4 @@ export const DAY = HOUR * 24;
* console.log(WEEK); // => 604800000 (7 * 24 * 60 * 60 * 1000)
* ```
*/
export const WEEK = DAY * 7;
export const WEEK: number = DAY * 7;
2 changes: 1 addition & 1 deletion dotenv/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @param object object to be stringified
* @returns string of object
*/
export function stringify(object: Record<string, string>) {
export function stringify(object: Record<string, string>): string {
const lines: string[] = [];
for (const [key, value] of Object.entries(object)) {
let quote;
Expand Down
2 changes: 1 addition & 1 deletion expect/fn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import { MOCK_SYMBOL, MockCall } from "./_mock_util.ts";

export function fn(...stubs: Function[]) {
export function fn(...stubs: Function[]): Function {
const calls: MockCall[] = [];

const f = (...args: any[]) => {
Expand Down
2 changes: 1 addition & 1 deletion fmt/colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ const ANSI_PATTERN = new RegExp(
* Remove ANSI escape codes from the string.
* @param string to remove ANSI escape codes from
*/
export const stripColor = stripAnsiCode;
export const stripColor: typeof stripAnsiCode = stripAnsiCode;

/**
* Remove ANSI escape codes from the string.
Expand Down
4 changes: 2 additions & 2 deletions front_matter/any.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

import { createExtractor, Parser } from "./create_extractor.ts";
import { createExtractor, type Extractor, Parser } from "./create_extractor.ts";
import { parse as parseYAML } from "../yaml/parse.ts";
import { parse as parseTOML } from "../toml/parse.ts";

Expand All @@ -10,7 +10,7 @@ export {
test,
} from "./test.ts";

export const extract = createExtractor({
export const extract: Extractor = createExtractor({
yaml: parseYAML as Parser,
toml: parseTOML as Parser,
json: JSON.parse as Parser,
Expand Down
6 changes: 4 additions & 2 deletions front_matter/json.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

import { createExtractor, Parser } from "./create_extractor.ts";
import { createExtractor, type Extractor, Parser } from "./create_extractor.ts";
import { test as _test } from "./test.ts";

export { Format } from "./_formats.ts";
Expand All @@ -10,6 +10,8 @@ export function test(str: string): boolean {
return _test(str, ["json"]);
}

export const extract = createExtractor({ json: JSON.parse as Parser });
export const extract: Extractor = createExtractor({
json: JSON.parse as Parser,
});
/** @deprecated (will be removed after 0.210.0) Import {@linkcode extract} as a named import instead. */
export default extract;
6 changes: 4 additions & 2 deletions front_matter/toml.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

import { createExtractor, Parser } from "./create_extractor.ts";
import { createExtractor, type Extractor, Parser } from "./create_extractor.ts";
import { test as _test } from "./test.ts";
import { parse } from "../toml/parse.ts";

Expand All @@ -11,6 +11,8 @@ export function test(str: string): boolean {
return _test(str, ["toml"]);
}

export const extract = createExtractor({ ["toml"]: parse as Parser });
export const extract: Extractor = createExtractor({
["toml"]: parse as Parser,
});
/** @deprecated (will be removed after 0.210.0) Import {@linkcode extract} as a named import instead. */
export default extract;
6 changes: 4 additions & 2 deletions front_matter/yaml.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

import { createExtractor, Parser } from "./create_extractor.ts";
import { createExtractor, type Extractor, Parser } from "./create_extractor.ts";
import { test as _test } from "./test.ts";
import { parse } from "../yaml/parse.ts";

Expand All @@ -11,6 +11,8 @@ export function test(str: string): boolean {
return _test(str, ["yaml"]);
}

export const extract = createExtractor({ ["yaml"]: parse as Parser });
export const extract: Extractor = createExtractor({
["yaml"]: parse as Parser,
});
/** @deprecated (will be removed after 0.210.0) Import {@linkcode extract} as a named import instead. */
export default extract;
4 changes: 2 additions & 2 deletions html/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const rawRe = new RegExp(`[${[...rawToEntity.keys()].join("")}]`, "g");
* assertEquals(escape("þð"), "þð");
* ```
*/
export function escape(str: string) {
export function escape(str: string): string {
return str.replaceAll(rawRe, (m) => rawToEntity.get(m)!);
}

Expand Down Expand Up @@ -73,7 +73,7 @@ const entityListRegexCache = new WeakMap<EntityList, RegExp>();
export function unescape(
str: string,
options: Partial<UnescapeOptions> = {},
) {
): string {
const { entityList } = { ...defaultUnescapeOptions, ...options };

let entityRe = entityListRegexCache.get(entityList);
Expand Down
9 changes: 5 additions & 4 deletions http/cookie_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ export type SecureCookieMapSetDeleteOptions = SecureCookieMapSetDeleteOptions_;
*
* @deprecated (will be removed after 0.212.0) Use {@link https://deno.land/std/http/cookie.ts} instead.
*/
export const cookieMapHeadersInitSymbol = cookieMapHeadersInitSymbol_;
export const cookieMapHeadersInitSymbol: typeof cookieMapHeadersInitSymbol_ =
cookieMapHeadersInitSymbol_;

/**
* Allows merging of various sources of headers into a final set of headers
Expand All @@ -159,7 +160,7 @@ export const cookieMapHeadersInitSymbol = cookieMapHeadersInitSymbol_;
*
* @deprecated (will be removed after 0.212.0) Use {@link https://deno.land/std/http/cookie.ts} instead.
*/
export const mergeHeaders = mergeHeaders_;
export const mergeHeaders: typeof mergeHeaders_ = mergeHeaders_;

/**
* Provides a way to manage cookies in a request and response on the server
Expand All @@ -174,7 +175,7 @@ export const mergeHeaders = mergeHeaders_;
*
* @deprecated (will be removed after 0.212.0) Use {@link https://deno.land/std/http/cookie.ts} instead.
*/
export const CookieMap = CookieMap_;
export const CookieMap: typeof CookieMap_ = CookieMap_;

/**
* Types of data that can be signed cryptographically.
Expand Down Expand Up @@ -211,4 +212,4 @@ export type KeyRing = KeyRing_;
*
* @deprecated (will be removed after 0.212.0) Use {@link https://deno.land/std/http/unstable_signed_cookie.ts} instead.
*/
export const SecureCookieMap = SecureCookieMap_;
export const SecureCookieMap: typeof SecureCookieMap_ = SecureCookieMap_;
5 changes: 4 additions & 1 deletion http/file_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,10 @@ export interface ServeDirOptions {
*
* @param req The request to handle
*/
export async function serveDir(req: Request, opts: ServeDirOptions = {}) {
export async function serveDir(
req: Request,
opts: ServeDirOptions = {},
): Promise<Response> {
let response: Response;
try {
response = await createServeDirResponse(req, opts);
Expand Down
21 changes: 13 additions & 8 deletions http/http_status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ import * as status from "./status.ts";
/**
* @deprecated (will be removed after 0.210.0) Import from {@link https://deno.land/std/http/status.ts} instead.
*/
export const Status = status.Status;
export const Status: typeof status.Status = status.Status;
/**
* @deprecated (will be removed after 0.210.0) Import from {@link https://deno.land/std/http/status.ts} instead.
*/
export type Status = status.Status;
/**
* @deprecated (will be removed after 0.210.0) Import from {@link https://deno.land/std/http/status.ts} instead.
*/
export const STATUS_TEXT = status.STATUS_TEXT;
export const STATUS_TEXT: typeof status.STATUS_TEXT = status.STATUS_TEXT;
/**
* @deprecated (will be removed after 0.210.0) Import from {@link https://deno.land/std/http/status.ts} instead.
*/
Expand Down Expand Up @@ -74,24 +74,29 @@ export type ErrorStatus = status.ErrorStatus;
/**
* @deprecated (will be removed after 0.210.0) Import from {@link https://deno.land/std/http/status.ts} instead.
*/
export const isInformationalStatus = status.isInformationalStatus;
export const isInformationalStatus: typeof status.isInformationalStatus =
status.isInformationalStatus;
/**
* @deprecated (will be removed after 0.210.0) Import from {@link https://deno.land/std/http/status.ts} instead.
*/
export const isSuccessfulStatus = status.isSuccessfulStatus;
export const isSuccessfulStatus: typeof status.isSuccessfulStatus =
status.isSuccessfulStatus;
/**
* @deprecated (will be removed after 0.210.0) Import from {@link https://deno.land/std/http/status.ts} instead.
*/
export const isRedirectStatus = status.isRedirectStatus;
export const isRedirectStatus: typeof status.isRedirectStatus =
status.isRedirectStatus;
/**
* @deprecated (will be removed after 0.210.0) Import from {@link https://deno.land/std/http/status.ts} instead.
*/
export const isClientErrorStatus = status.isClientErrorStatus;
export const isClientErrorStatus: typeof status.isClientErrorStatus =
status.isClientErrorStatus;
/**
* @deprecated (will be removed after 0.210.0) Import from {@link https://deno.land/std/http/status.ts} instead.
*/
export const isServerErrorStatus = status.isServerErrorStatus;
export const isServerErrorStatus: typeof status.isServerErrorStatus =
status.isServerErrorStatus;
/**
* @deprecated (will be removed after 0.210.0) Import from {@link https://deno.land/std/http/status.ts} instead.
*/
export const isErrorStatus = status.isErrorStatus;
export const isErrorStatus: typeof status.isErrorStatus = status.isErrorStatus;
12 changes: 6 additions & 6 deletions http/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export class Server {
*
* @param listener The listener to accept connections from.
*/
async serve(listener: Deno.Listener) {
async serve(listener: Deno.Listener): Promise<void> {
if (this.#closed) {
throw new Deno.errors.Http(ERROR_SERVER_CLOSED);
}
Expand Down Expand Up @@ -188,7 +188,7 @@ export class Server {
* await server.listenAndServe();
* ```
*/
async listenAndServe() {
async listenAndServe(): Promise<void> {
if (this.#closed) {
throw new Deno.errors.Http(ERROR_SERVER_CLOSED);
}
Expand Down Expand Up @@ -238,7 +238,7 @@ export class Server {
* @param certFile The path to the file containing the TLS certificate.
* @param keyFile The path to the file containing the TLS private key.
*/
async listenAndServeTls(certFile: string, keyFile: string) {
async listenAndServeTls(certFile: string, keyFile: string): Promise<void> {
if (this.#closed) {
throw new Deno.errors.Http(ERROR_SERVER_CLOSED);
}
Expand Down Expand Up @@ -559,7 +559,7 @@ export async function serveListener(
listener: Deno.Listener,
handler: Handler,
options?: ServeListenerOptions,
) {
): Promise<void> {
const server = new Server({ handler, onError: options?.onError });

options?.signal?.addEventListener("abort", () => server.close(), {
Expand Down Expand Up @@ -626,7 +626,7 @@ function hostnameForDisplay(hostname: string) {
export async function serve(
handler: Handler,
options: ServeInit = {},
) {
): Promise<void> {
let port = options.port ?? 8000;
if (typeof port !== "number") {
port = Number(port);
Expand Down Expand Up @@ -744,7 +744,7 @@ export interface ServeTlsInit extends ServeInit {
export async function serveTls(
handler: Handler,
options: ServeTlsInit,
) {
): Promise<void> {
if (!options.key && !options.keyFile) {
throw new Error("TLS config is given, but 'key' is missing.");
}
Expand Down
5 changes: 3 additions & 2 deletions http/unstable_cookie_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ class Cookie implements CookieAttributes {
*
* @deprecated (will be removed in 0.212.0) Use {@link https://deno.land/std/http/cookie.ts} instead.
*/
export const cookieMapHeadersInitSymbol = Symbol.for(
export const cookieMapHeadersInitSymbol: unique symbol = Symbol.for(
"Deno.std.cookieMap.headersInit",
);

Expand Down Expand Up @@ -373,7 +373,8 @@ const isSecure = Symbol("#secure");
const requestKeys = Symbol("#requestKeys");

/** An internal abstract class which provides common functionality for
* {@link CookieMap} and {@link SecureCookieMap}. */
* {@link CookieMap} and {@link SecureCookieMap}.
*/
abstract class CookieMapBase implements Mergeable {
[keys]?: string[];
[requestHeaders]: Headers;
Expand Down
9 changes: 8 additions & 1 deletion http/user_agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1054,7 +1054,14 @@ export class UserAgent {
return this.#ua;
}

toJSON() {
toJSON(): {
browser: Browser;
cpu: Cpu;
device: Device;
engine: Engine;
os: Os;
ua: string;
} {
const { browser, cpu, device, engine, os, ua } = this;
return { browser, cpu, device, engine, os, ua };
}
Expand Down
Loading

0 comments on commit b080361

Please sign in to comment.