Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Upgrade TypeScript from 3.2.2 to 3.6.4 #7

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@
"tslint": "^5.11.0",
"tslint-language-service": "^0.9.9",
"tslint-no-unused": "^0.2.0-alpha.1",
"typescript": "^3.2.2"
"typescript": "^3.6.4"
}
}
4 changes: 1 addition & 3 deletions src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
const tuple = <T extends Array<unknown>>(...args: T): T => args;

// `Object.keys` does not return the keys as string literals, only strings. Use this helper as a
// workaround. https://github.com/Microsoft/TypeScript/pull/12253#issuecomment-263132208
const keys = <O extends object>(obj: O) => Object.keys(obj) as Array<keyof O>;

// `Object.entries` is ES2017, but this lib only supports ES2015 at runtime, so we must define our
// own version.
const entries = <K extends string, V>(obj: Record<K, V>) =>
keys(obj).map(key => tuple(key, obj[key]));
keys(obj).map(key => [key, obj[key]] as const);
const fromEntries = <K extends string, V>(arr: Array<[K, V]>) =>
// `Object.assign` is poorly typed: it returns `any` when spreading. Use cast to workaround.
Object.assign({}, ...arr.map(([k, v]) => ({ [k]: v }))) as Record<K, V>;
Expand Down
17 changes: 12 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,25 @@ export type ImgixUrlQueryParams = {
faceindex?: number;
facepad?: number;
};

const pickTrueInObject = <K extends string>(obj: Record<K, boolean>): Partial<Record<K, true>> =>
pickBy(obj, (_key, value): value is true => value);
// TODO: remove cast and either make all functions allow partial, or all non-partial.
// const pickTrueInObject = <K extends string>(obj: Record<K, boolean>) =>
const pickTrueInObject = <K extends string>(
obj: Partial<Record<K, boolean>>,
): Partial<Record<K, true>> =>
pickBy(obj as Record<K, boolean>, (_key, value): value is true => value);
Copy link
Member Author

@OliverJAsh OliverJAsh Oct 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit stuck here.

pickTrueInObject must be allowed to receive partials. But if we update this function, we have to update every helper it uses internally (pickBy and entries) to also allow Partial. That feels wrong :-/

@karol-majewski Any thoughts on what we should do here?

Complete example:

const keys = <O extends object>(obj: O) =>
    Object.keys(obj) as Array<keyof O>;

const entries = <K extends string, V>(obj: Record<K, V>) =>
    keys(obj).map(key => [key, obj[key]] as const);

const fromEntries = <K extends string, V>(arr: Array<[K, V]>) =>
    Object.assign({}, ...arr.map(([k, v]) => ({ [k]: v }))) as Record<K, V>;

const pickBy = <K extends string, V, Result extends V>(
    obj: Record<K, V>,
    predicate: (key: K, value: V) => value is Result,
) =>
    fromEntries(
        entries(obj).filter((entry): entry is [K, Result] => {
            const [key, value] = entry;
            return predicate(key, value);
        }),
    );

const pickTrueInObject = <K extends string>(
    obj: Record<K, boolean>,
): Partial<Record<K, true>> =>
    pickBy(obj, (_key, value): value is true => value);

declare const record: Partial<Record<'foo' | 'bar', boolean>>;
// declare const record: Record<'foo' | 'bar', boolean>;

pickTrueInObject(record); // error

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@karol-majewski Hey, do you have any thoughts on the above?

Copy link

@karol-majewski karol-majewski Nov 13, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the T passed to pickTrueInObject is a Dictionary<boolean>, isn't it sufficient to say pickTrueInObject will return a Partial<T>?

Sure, the signature could be more precise:

declare function pickTrueInObject<T extends object>(source: T): Partial<Record<PropertyOfValue<T, boolean>, true>>;

type PropertyOfValue<T, V> = {
    [K in keyof T]: T[K] extends V
      ? K
      : never
  }[keyof T];


declare const foo: {
  foo: number;
  bar: boolean;
}

let result = pickTrueInObject(foo); // { bar?: true }

but since we're not dealing with object literals, we cannot use the type system to tell us which properties will be true in runtime and that renders having this extra information useless.

Copy link

@karol-majewski karol-majewski Nov 13, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to pipe well.

import { pipe } from 'pipe-ts';

declare function pickTrueInObject<T extends object>(source: T): Partial<Record<PropertyOfValue<T, boolean>, true>>;

type PropertyOfValue<T, V> = {
    [K in keyof T]: T[K] extends V
      ? K
      : never
  }[keyof T];


declare const foo: {
  foo: number;
  bar: boolean;
}

const UNSAFE_keys = <T extends object>(source: T) =>
  Object.keys(source) as (keyof T)[];

const keys = pipe(pickTrueInObject, UNSAFE_keys)(foo); // "bar"[]

const pickTrueObjectKeys = pipe(
pickTrueInObject,
// tslint:disable-next-line no-unbound-method
Object.keys,
// This function has an overload which breaks type inference. See:
// https://github.com/Microsoft/TypeScript/issues/29904#issuecomment-471105473. We workaround this
// by wrapping the function.
obj => Object.keys(obj),
);
const undefinedIfEmptyString = (str: string): string | undefined => (str === '' ? undefined : str);
const joinWithComma = (strs: string[]) => strs.join(',');
const serializeImgixUrlQueryParamListValue = pipe(
pickTrueObjectKeys,
// Needed to workaround https://github.com/microsoft/TypeScript/issues/29904#issuecomment-543785534
value => value,
joinWithComma,
undefinedIfEmptyString,
);
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -319,10 +319,10 @@ tsutils@^2.27.2:
dependencies:
tslib "^1.8.1"

typescript@^3.2.2:
version "3.2.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.2.2.tgz#fe8101c46aa123f8353523ebdcf5730c2ae493e5"
integrity sha512-VCj5UiSyHBjwfYacmDuc/NOk4QQixbE+Wn7MFJuS0nRuPQbof132Pw4u53dm264O8LPc2MVsc7RJNml5szurkg==
typescript@^3.6.4:
version "3.6.4"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.6.4.tgz#b18752bb3792bc1a0281335f7f6ebf1bbfc5b91d"
integrity sha512-unoCll1+l+YK4i4F8f22TaNVPRHcD9PA3yCuZ8g5e0qGqlVlJ/8FSateOLLSagn+Yg5+ZwuPkL8LFUc0Jcvksg==

url-transformers@^0.0.5:
version "0.0.5"
Expand Down