Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set exactOptionalPropertyTypes: true, improved types #725

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
6 changes: 6 additions & 0 deletions benchmark/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ const serverAdapter = createServerAdapter(async req => {
return Response.json({ message: `Hello, ${name}!` });
});

// @ts-ignore Type 'ServerAdapter<{}, ServerAdapterBaseObject<{}, (req: Request) => Promise<Response>>>' is not assignable to type 'RequestListener<typeof IncomingMessage, typeof ServerResponse>'.
// Types of parameters 'req' and 'req' are incompatible.
// Type 'IncomingMessage' is not assignable to type 'NodeRequest' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
// Types of property 'url' are incompatible.
// Type 'string | undefined' is not assignable to type 'string'.
// Type 'undefined' is not assignable to type 'string'.
createServer(serverAdapter).listen(4000, () => {
console.log('listening on 0.0.0.0:4000');
});
4 changes: 2 additions & 2 deletions e2e/shared-scripts/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { OutputValue, Stack } from '@pulumi/pulumi/automation';

export type DeploymentConfiguration<TProgramOutput = any> = {
name: string;
prerequisites?: (stack: Stack) => Promise<void>;
config?: (stack: Stack) => Promise<void>;
prerequisites?: ((stack: Stack) => Promise<void>) | undefined;
config?: ((stack: Stack) => Promise<void>) | undefined;
program: () => Promise<{
[K in keyof TProgramOutput]: Output<TProgramOutput[K]> | TProgramOutput[K];
}>;
Expand Down
6 changes: 5 additions & 1 deletion e2e/vercel/scripts/createVercelDeployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ class VercelProvider implements pulumi.dynamic.ResourceProvider {
export class VercelDeployment extends pulumi.dynamic.Resource {
public readonly url!: pulumi.Output<string>;

constructor(name: string, props: VercelDeploymentInputs, opts?: pulumi.CustomResourceOptions) {
constructor(
name: string,
props: VercelDeploymentInputs,
opts?: pulumi.CustomResourceOptions | undefined,
) {
super(
new VercelProvider(),
name,
Expand Down
10 changes: 6 additions & 4 deletions packages/cookie-store/src/CookieStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from './types.js';

export class CookieStore extends EventTarget {
onchange?: (event: CookieChangeEvent) => void;
onchange?: ((event: CookieChangeEvent) => void) | undefined;

private cookieMap = new Map<string, Cookie>();

Expand All @@ -22,7 +22,7 @@ export class CookieStore extends EventTarget {
}

async get(
init?: CookieStoreGetOptions['name'] | CookieStoreGetOptions,
init?: CookieStoreGetOptions['name'] | CookieStoreGetOptions | undefined,
): Promise<Cookie | undefined> {
if (init == null) {
throw new TypeError('CookieStoreGetOptions must not be empty');
Expand All @@ -32,7 +32,7 @@ export class CookieStore extends EventTarget {
return (await this.getAll(init))[0];
}

async set(init: CookieListItem | string, possibleValue?: string): Promise<void> {
async set(init: CookieListItem | string, possibleValue?: string | undefined): Promise<void> {
const item: CookieListItem = {
name: '',
value: '',
Expand Down Expand Up @@ -96,7 +96,9 @@ export class CookieStore extends EventTarget {
}
}

async getAll(init?: CookieStoreGetOptions['name'] | CookieStoreGetOptions): Promise<Cookie[]> {
async getAll(
init?: CookieStoreGetOptions['name'] | CookieStoreGetOptions | undefined,
): Promise<Cookie[]> {
const cookies = Array.from(this.cookieMap.values());
if (init == null || Object.keys(init).length === 0) {
return cookies;
Expand Down
2 changes: 1 addition & 1 deletion packages/cookie-store/src/parse.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Cookie } from './types.js';

export interface ParseOptions {
decode?: boolean;
decode?: boolean | undefined;
}

const decode = decodeURIComponent;
Expand Down
32 changes: 16 additions & 16 deletions packages/cookie-store/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
export interface Cookie {
domain?: string;
expires?: number;
domain?: string | undefined;
expires?: number | undefined;
name: string;
path?: string;
secure?: boolean;
sameSite?: CookieSameSite;
path?: string | undefined;
secure?: boolean | undefined;
sameSite?: CookieSameSite | undefined;
value: string;
httpOnly?: boolean;
httpOnly?: boolean | undefined;
}

export interface CookieStoreDeleteOptions {
name: string;
domain?: string;
path?: string;
domain?: string | undefined;
path?: string | undefined;
}

export interface CookieStoreGetOptions {
name?: string;
url?: string;
name?: string | undefined;
url?: string | undefined;
}

export type CookieSameSite = 'strict' | 'lax' | 'none';

export interface CookieListItem {
name?: string;
value?: string;
name?: string | undefined;
value?: string | undefined;
domain: string | null;
path?: string;
path?: string | undefined;
expires: Date | number | null;
secure?: boolean;
sameSite?: CookieSameSite;
httpOnly?: boolean;
secure?: boolean | undefined;
sameSite?: CookieSameSite | undefined;
httpOnly?: boolean | undefined;
}

export type CookieList = CookieListItem[];
Expand Down
22 changes: 11 additions & 11 deletions packages/fetch/dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ declare module '@whatwg-node/fetch' {
export const fetch: typeof globalThis.fetch;
export const Request: typeof globalThis.Request;
export const Response: typeof globalThis.Response & {
json(data: any, init?: ResponseInit): globalThis.Response;
json(data: any, init?: ResponseInit | undefined): globalThis.Response;
};
export const Headers: typeof globalThis.Headers;
export const FormData: typeof globalThis.FormData;
Expand All @@ -30,24 +30,24 @@ declare module '@whatwg-node/fetch' {
export const URLPattern: _URLPattern;
export interface FormDataLimits {
/* Max field name size (in bytes). Default: 100. */
fieldNameSize?: number;
fieldNameSize?: number | undefined;
/* Max field value size (in bytes). Default: 1MB. */
fieldSize?: number;
fieldSize?: number | undefined;
/* Max number of fields. Default: Infinity. */
fields?: number;
fields?: number | undefined;
/* For multipart forms, the max file size (in bytes). Default: Infinity. */
fileSize?: number;
fileSize?: number | undefined;
/* For multipart forms, the max number of file fields. Default: Infinity. */
files?: number;
files?: number | undefined;
/* For multipart forms, the max number of parts (fields + files). Default: Infinity. */
parts?: number;
parts?: number | undefined;
/* For multipart forms, the max number of header key-value pairs to parse. Default: 2000. */
headerSize?: number;
headerSize?: number | undefined;
}
export const createFetch: (opts?: {
useNodeFetch?: boolean;
formDataLimits?: FormDataLimits;
}) => {
useNodeFetch?: boolean | undefined;
formDataLimits?: FormDataLimits | undefined;
} | undefined) => {
fetch: typeof fetch;
Request: typeof Request;
Response: typeof Response;
Expand Down
4 changes: 2 additions & 2 deletions packages/fetchache/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,11 @@ export interface KeyValueCacheSetOptions {
* Specified in **seconds**, the time-to-live (TTL) value limits the lifespan
* of the data being stored in the cache.
*/
ttl?: number | null;
ttl?: number | null | undefined;
}

export interface KeyValueCache<V = any> {
get(key: string): Promise<V | undefined>;
set(key: string, value: V, options?: KeyValueCacheSetOptions): Promise<void>;
set(key: string, value: V, options?: KeyValueCacheSetOptions | undefined): Promise<void>;
delete(key: string): Promise<boolean | void>;
}
4 changes: 2 additions & 2 deletions packages/node-fetch/src/Blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class PonyfillBlob implements Blob {
private encoding: BufferEncoding;
constructor(
private blobParts: BlobPart[],
options?: BlobOptions,
options?: BlobOptions | undefined,
) {
this.type = options?.type || 'application/octet-stream';
this.encoding = options?.encoding || 'utf8';
Expand Down Expand Up @@ -131,5 +131,5 @@ export class PonyfillBlob implements Blob {

export interface PonyfillBlob {
prototype: Blob;
new (blobParts?: BlobPart[], options?: BlobPropertyBag): Blob;
new (blobParts?: BlobPart[] | undefined, options?: BlobPropertyBag | undefined): Blob;
}
22 changes: 11 additions & 11 deletions packages/node-fetch/src/Body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ export type BodyPonyfillInit =

export interface FormDataLimits {
/* Max field name size (in bytes). Default: 100. */
fieldNameSize?: number;
fieldNameSize?: number | undefined;
/* Max field value size (in bytes). Default: 1MB. */
fieldSize?: number;
fieldSize?: number | undefined;
/* Max number of fields. Default: Infinity. */
fields?: number;
fields?: number | undefined;
/* For multipart forms, the max file size (in bytes). Default: Infinity. */
fileSize?: number;
fileSize?: number | undefined;
/* For multipart forms, the max number of file fields. Default: Infinity. */
files?: number;
files?: number | undefined;
/* For multipart forms, the max number of parts (fields + files). Default: Infinity. */
parts?: number;
parts?: number | undefined;
/* For multipart forms, the max number of header key-value pairs to parse. Default: 2000. */
headerSize?: number;
headerSize?: number | undefined;
}

export interface PonyfillBodyOptions {
formDataLimits?: FormDataLimits;
formDataLimits?: FormDataLimits | undefined;
}

export class PonyfillBody<TJSON = any> implements Body {
Expand All @@ -60,7 +60,7 @@ export class PonyfillBody<TJSON = any> implements Body {
this.bodyType = bodyType;
}

private bodyType?: BodyInitType;
private bodyType?: BodyInitType | undefined;

private _bodyFactory: () => PonyfillReadableStream<Uint8Array> | null = () => null;
private _generatedBody: PonyfillReadableStream<Uint8Array> | null = null;
Expand Down Expand Up @@ -169,7 +169,7 @@ export class PonyfillBody<TJSON = any> implements Body {
});
}

formData(opts?: { formDataLimits: FormDataLimits }): Promise<PonyfillFormData> {
formData(opts?: { formDataLimits: FormDataLimits } | undefined): Promise<PonyfillFormData> {
if (this.bodyType === BodyInitType.FormData) {
return Promise.resolve(this.bodyInit as PonyfillFormData);
}
Expand Down Expand Up @@ -280,7 +280,7 @@ export class PonyfillBody<TJSON = any> implements Body {
}

function processBodyInit(bodyInit: BodyPonyfillInit | null): {
bodyType?: BodyInitType;
bodyType?: BodyInitType | undefined;
contentType: string | null;
contentLength: number | null;
bodyFactory(): PonyfillReadableStream<Uint8Array> | null;
Expand Down
2 changes: 1 addition & 1 deletion packages/node-fetch/src/File.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export class PonyfillFile extends PonyfillBlob implements File {
constructor(
fileBits: BlobPart[],
public name: string,
options?: FilePropertyBag,
options?: FilePropertyBag | undefined,
) {
super(fileBits, options);
this.lastModified = options?.lastModified || Date.now();
Expand Down
10 changes: 5 additions & 5 deletions packages/node-fetch/src/FormData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export class PonyfillFormData implements FormData {
private map = new Map<string, FormDataEntryValue[]>();

append(name: string, value: string): void;
append(name: string, value: PonyfillBlob, fileName?: string): void;
append(name: string, value: PonyfillBlob | string, fileName?: string): void {
append(name: string, value: PonyfillBlob, fileName?: string | undefined): void;
append(name: string, value: PonyfillBlob | string, fileName?: string | undefined): void {
let values = this.map.get(name);
if (!values) {
values = [];
Expand Down Expand Up @@ -37,8 +37,8 @@ export class PonyfillFormData implements FormData {
}

set(name: string, value: string): void;
set(name: string, value: PonyfillBlob, fileName?: string): void;
set(name: string, value: PonyfillBlob | string, fileName?: string): void {
set(name: string, value: PonyfillBlob, fileName?: string | undefined): void;
set(name: string, value: PonyfillBlob | string, fileName?: string | undefined): void {
const entry: FormDataEntryValue = isBlob(value)
? getNormalizedFile(name, value, fileName)
: value;
Expand Down Expand Up @@ -133,7 +133,7 @@ export function getStreamFromFormData(
});
}

function getNormalizedFile(name: string, blob: PonyfillBlob, fileName?: string) {
function getNormalizedFile(name: string, blob: PonyfillBlob, fileName?: string | undefined) {
if (blob instanceof PonyfillFile) {
if (fileName != null) {
return new PonyfillFile([blob], fileName, {
Expand Down
2 changes: 1 addition & 1 deletion packages/node-fetch/src/Headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class PonyfillHeaders implements Headers {
private objectNormalizedKeysOfHeadersInit: string[] = [];
private objectOriginalKeysOfHeadersInit: string[] = [];

constructor(private headersInit?: PonyfillHeadersInit) {}
constructor(private headersInit?: PonyfillHeadersInit | undefined) {}

// perf: we don't need to build `this.map` for Requests, as we can access the headers directly
private _get(key: string) {
Expand Down
2 changes: 1 addition & 1 deletion packages/node-fetch/src/ReadableStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export class PonyfillReadableStream<T> implements ReadableStream<T> {

getReader(options: { mode: 'byob' }): ReadableStreamBYOBReader;
getReader(): ReadableStreamDefaultReader<T>;
getReader(_options?: ReadableStreamGetReaderOptions): ReadableStreamReader<T> {
getReader(_options?: ReadableStreamGetReaderOptions | undefined): ReadableStreamReader<T> {
const iterator = this.readable[Symbol.asyncIterator]();
this.locked = true;
return {
Expand Down
16 changes: 8 additions & 8 deletions packages/node-fetch/src/Request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ function isRequest(input: any): input is PonyfillRequest {

export type RequestPonyfillInit = PonyfillBodyOptions &
Omit<RequestInit, 'body' | 'headers'> & {
body?: BodyPonyfillInit | null;
headers?: PonyfillHeadersInit;
headersSerializer?: HeadersSerializer;
agent?: Agent;
body?: BodyPonyfillInit | null | undefined;
headers?: PonyfillHeadersInit | undefined;
headersSerializer?: HeadersSerializer | undefined;
agent?: Agent | undefined;
};

type HeadersSerializer = (
headers: Headers,
onContentLength?: (contentLength: string) => void,
onContentLength?: ((contentLength: string) => void) | undefined,
) => string[];

export class PonyfillRequest<TJSON = any> extends PonyfillBody<TJSON> implements Request {
constructor(input: RequestInfo | URL, options?: RequestPonyfillInit) {
constructor(input: RequestInfo | URL, options?: RequestPonyfillInit | undefined) {
let url: string | undefined;
let bodyInit: BodyPonyfillInit | null = null;
let requestInit: RequestPonyfillInit | undefined;
Expand Down Expand Up @@ -85,7 +85,7 @@ export class PonyfillRequest<TJSON = any> extends PonyfillBody<TJSON> implements
}
}

headersSerializer?: HeadersSerializer;
headersSerializer?: HeadersSerializer | undefined;
cache: RequestCache;
credentials: RequestCredentials;
destination: RequestDestination;
Expand All @@ -99,7 +99,7 @@ export class PonyfillRequest<TJSON = any> extends PonyfillBody<TJSON> implements
referrer: string;
referrerPolicy: ReferrerPolicy;
url: string;
agent?: Agent;
agent?: Agent | undefined;

private _signal: AbortSignal | undefined | null;

Expand Down
10 changes: 5 additions & 5 deletions packages/node-fetch/src/Response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import { isHeadersLike, PonyfillHeaders, PonyfillHeadersInit } from './Headers.j

export type ResponsePonyfilInit = PonyfillBodyOptions &
Omit<ResponseInit, 'headers'> & {
url?: string;
redirected?: boolean;
headers?: PonyfillHeadersInit;
type?: ResponseType;
url?: string | undefined;
redirected?: boolean | undefined;
headers?: PonyfillHeadersInit | undefined;
type?: ResponseType | undefined;
};

const JSON_CONTENT_TYPE = 'application/json; charset=utf-8';

export class PonyfillResponse<TJSON = any> extends PonyfillBody<TJSON> implements Response {
headers: Headers;

constructor(body?: BodyPonyfillInit | null, init?: ResponsePonyfilInit) {
constructor(body?: BodyPonyfillInit | null | undefined, init?: ResponsePonyfilInit | undefined) {
super(body || null, init);
this.headers =
init?.headers && isHeadersLike(init.headers)
Expand Down
Loading