Skip to content

Commit

Permalink
prefer type-safer/custom Omit vs built-it TS type
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimiry committed Jul 5, 2019
1 parent 0dbab8f commit 21d5f1e
Show file tree
Hide file tree
Showing 25 changed files with 168 additions and 36 deletions.
9 changes: 9 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
},
"extends": [
"./linting-extending/eslint/typescript-eslint.json"
]
}
7 changes: 2 additions & 5 deletions .lintstagedrc.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
{
"*.ts": [
"import-sort --write",
"yarn run lint:ts:base --fix",
"git add"
],
"*.js": [
"yarn run lint:js:base --fix",
"yarn run lint:ts:base:tslint --fix",
"yarn run lint:ts:base:eslint --fix",
"git add"
]
}
16 changes: 16 additions & 0 deletions linting-extending/eslint/typescript-eslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint"
],
"rules": {
"@typescript-eslint/ban-types": [
"error",
{
"types": {
"Omit": "Prefer `Skip` to built-in TS `Omit` type."
}
}
]
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"rulesDirectory": [
"./../node_modules/codelyzer"
"./../../node_modules/codelyzer"
],
"rules": {
"directive-selector": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"rulesDirectory": [
"./../node_modules/tslint-consistent-codestyle/rules"
"./../../node_modules/tslint-consistent-codestyle/rules"
],
"rules": {
"early-exit": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"rulesDirectory": [
"./../node_modules/tslint-eslint-rules/dist/rules"
"./../../node_modules/tslint-eslint-rules/dist/rules"
],
"rules": {
"object-curly-spacing": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"rulesDirectory": [
"./../node_modules/tslint-rules-bunch/rules"
"./../../node_modules/tslint-rules-bunch/rules"
],
"rules": {
"no-import-zones": [
Expand Down
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@
"electron-builder:dist:linux:pacman": "electron-builder --x64 --linux pacman",
"electron-builder:dist:linux:rpm": "electron-builder --x64 --linux rpm",
"lint": "npm-run-all lint:sass lint:code",
"lint:code": "npm-run-all lint:js lint:ts",
"lint:js:base": "tslint",
"lint:js": "yarn run lint:js:base \"./src/**/*.js\" \"./*.js\"",
"lint:code": "npm-run-all lint:ts:tslint lint:ts:eslint",
"lint:sass": "sass-lint -v -q -c ./sass-lint.yml",
"lint:ts:base": "tslint -p ./tsconfig.json",
"lint:ts": "yarn run lint:ts:base \"./src/**/*.ts\" \"./scripts/**/*.ts\" \"./webpack/**/*.ts\"",
"lint:ts:base:tslint": "tslint -p ./tsconfig.json",
"lint:ts:base:eslint": "eslint",
"lint:ts:tslint": "yarn run lint:ts:base:tslint \"./src/**/*.ts\" \"./scripts/**/*.ts\" \"./webpack/**/*.ts\"",
"lint:ts:eslint": "yarn run lint:ts:base:eslint \"./src/**/*.ts\" \"./scripts/**/*.ts\" \"./webpack/**/*.ts\"",
"start:electron": "electron ./app/electron-main.js",
"start:electron:dev": "electron ./app-dev/electron-main.js",
"test:e2e": "cross-env TS_NODE_FILES=true ts-node -r tsconfig-paths/register ./scripts/ava-loader.ts --verbose --filesGlob \"./src/e2e/**/*.{spec,test}.ts\"",
Expand Down Expand Up @@ -190,6 +190,8 @@
"@types/webpack-env": "1.13.9",
"@types/webpack-merge": "4.1.5",
"@types/webpack-node-externals": "1.6.3",
"@typescript-eslint/eslint-plugin": "1.11.0",
"@typescript-eslint/parser": "1.11.0",
"app-builder-lib": "20.44.4",
"archiver": "3.0.0",
"ava": "2.1.0",
Expand All @@ -209,6 +211,7 @@
"electron": "5.0.6",
"electron-builder": "20.44.4",
"escape-string-regexp": "2.0.0",
"eslint": "6.0.1",
"exports-loader": "0.7.0",
"file-loader": "4.0.0",
"font-awesome": "4.7.0",
Expand Down
4 changes: 4 additions & 0 deletions src/@types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {Observable} from "rxjs";

import {Omit} from "type-fest";

declare global {
type Arguments<F extends (...x: any[]) => any> =
F extends (...x: infer A) => any ? A : never;
Expand All @@ -12,4 +14,6 @@ declare global {
T;

type Mutable<T> = { -readonly [K in keyof T]: T[K]; };

type Skip<T, K extends keyof T> = Omit<T, K>; // eslint-disable-line @typescript-eslint/ban-types
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export async function buildDbIndexingEndpoints(
}

export const narrowIndexActionPayload: (
payload: Omit<Extract<UnionOf<typeof IPC_MAIN_API_DB_INDEXER_NOTIFICATION_ACTIONS>, { type: "Index" }>["payload"], "uid">,
payload: Skip<Extract<UnionOf<typeof IPC_MAIN_API_DB_INDEXER_NOTIFICATION_ACTIONS>, { type: "Index" }>["payload"], "uid">,
) => typeof payload = (() => {
type Fn = typeof narrowIndexActionPayload;
type Mails = ReturnType<Fn>["add"];
Expand Down
2 changes: 1 addition & 1 deletion src/electron-main/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const SNAP_CONTAINER = (
);

export const INITIAL_STORES: Readonly<{
config: () => Omit<Config, "jsFlags"> & Required<Pick<Config, "jsFlags">>;
config: () => Skip<Config, "jsFlags"> & Required<Pick<Config, "jsFlags">>;
settings: () => Settings;
}> = Object.freeze({
config: () => {
Expand Down
2 changes: 1 addition & 1 deletion src/electron-main/database/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const resolveAccountFolders: <T extends keyof FsDb["accounts"]>(account:
export function patchMetadata(
target: FsDbAccount["metadata"],
// TODO TS: use patch: Arguments<IpcMainApiEndpoints["dbPatch"]>[0]["metadata"],
source: Omit<FsDbAccount<"protonmail">["metadata"], "type"> | Omit<FsDbAccount<"tutanota">["metadata"], "type">,
source: Skip<FsDbAccount<"protonmail">["metadata"], "type"> | Skip<FsDbAccount<"tutanota">["metadata"], "type">,
sourceType: "dbPatch" | "loadDatabase",
): boolean {
const logPrefix = `patchMetadata() ${sourceType}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {isLoggedIn, isUpsertOperationType, preprocessError} from "src/electron-p

interface DbPatchBundle {
patch: DbPatch;
metadata: Omit<FsDbAccount<"protonmail">["metadata"], "type">;
metadata: Skip<FsDbAccount<"protonmail">["metadata"], "type">;
}

type BuildDbPatchMethodReturnType = ProtonmailApiScan["ApiImplReturns"]["buildDbPatch"];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface MessageResponse extends Response {
export interface MessagesResponse extends Response {
Total: number;
Limit: number;
Messages: Array<Omit<Message, "Attachments" | "Body" | "Header" | "MIMEType" | "ParsedHeaders" | "ReplyTo" | "ReplyTos" | "SpamScore"> &
Messages: Array<Skip<Message, "Attachments" | "Body" | "Header" | "MIMEType" | "ParsedHeaders" | "ReplyTo" | "ReplyTos" | "SpamScore"> &
{ HasAttachment: NumberBoolean }>;
}

Expand All @@ -36,7 +36,7 @@ export interface ContactResponse extends Response {
export interface ContactsResponse extends Response {
Total: number;
Limit: number;
Contacts: Array<Omit<Contact, "Cards" | "ContactEmails">>;
Contacts: Array<Skip<Contact, "Cards" | "ContactEmails">>;
}

export interface LabelsResponse extends Response {
Expand Down
2 changes: 1 addition & 1 deletion src/electron-preload/webview/protonmail/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function isLoggedIn(): boolean {
}

export const preprocessError: Arguments<typeof buildDbPatchRetryPipeline>[0] = (rawError: any) => {
const sanitizedNgHttpResponse: (Omit<ng.IHttpResponse<"<wiped out>">, "headers"> & { message: string; headers: "<wiped out>" }) | false
const sanitizedNgHttpResponse: (Skip<ng.IHttpResponse<"<wiped out>">, "headers"> & { message: string; headers: "<wiped out>" }) | false
= angularJsHttpResponseTypeGuard(rawError)
? (() => {
const result = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {resolveProviderApi} from "src/electron-preload/webview/tutanota/lib/prov

interface DbPatchBundle {
patch: DbPatch;
metadata: Omit<FsDbAccount<"tutanota">["metadata"], "type">;
metadata: Skip<FsDbAccount<"tutanota">["metadata"], "type">;
}

type BuildDbPatchMethodReturnType = TutanotaScanApi["ApiImplReturns"]["buildDbPatch"];
Expand Down
4 changes: 2 additions & 2 deletions src/electron-preload/webview/tutanota/lib/rest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export async function fetchMultipleEntities<T extends BaseEntity<Id | IdTuple>>(
export async function fetchEntitiesRange<T extends BaseEntity<IdTuple>>(
typeRef: TypeRef<T>,
listId: T["_id"][0],
queryParams: Required<Omit<RequestParams, "ids">>,
queryParams: Required<Skip<RequestParams, "ids">>,
): Promise<T[]> {
logger.debug("fetchEntitiesRange()");

Expand All @@ -66,7 +66,7 @@ export async function fetchEntitiesRange<T extends BaseEntity<IdTuple>>(
export async function fetchEntitiesRangeUntilTheEnd<T extends BaseEntity<IdTuple>>(
typeRef: TypeRef<T>,
listId: T["_id"][0],
{start, count}: Required<Omit<RequestParams, "ids" | "reverse">>,
{start, count}: Required<Skip<RequestParams, "ids" | "reverse">>,
portionCallback: (entities: T[]) => Promise<void>,
): Promise<void> {
logger.debug("fetchEntitiesRangeUntilTheEnd()");
Expand Down
2 changes: 1 addition & 1 deletion src/shared/api/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const ENDPOINTS_DEFINITION = {
dbPatch: ActionType.Promise<DbModel.DbAccountPk
& { forceFlush?: boolean }
& { patch: DbPatch }
& { metadata: Omit<FsDbAccount<"protonmail">["metadata"], "type"> | Omit<FsDbAccount<"tutanota">["metadata"], "type"> },
& { metadata: Skip<FsDbAccount<"protonmail">["metadata"], "type"> | Skip<FsDbAccount<"tutanota">["metadata"], "type"> },
DbModel.FsDbAccount["metadata"]>(),

dbGetAccountMetadata: ActionType.Promise<DbModel.DbAccountPk, DbModel.FsDbAccount["metadata"] | null>(),
Expand Down
2 changes: 1 addition & 1 deletion src/shared/model/database/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export const CONTACT_SOCIAL_TYPE = buildEnumBundle({
CUSTOM: "5",
} as const);

export const INDEXABLE_MAIL_FIELDS_STUB_CONTAINER: Readonly<Record<keyof Omit<IndexableMail, "pk">, null>> = {
export const INDEXABLE_MAIL_FIELDS_STUB_CONTAINER: Readonly<Record<keyof Skip<IndexableMail, "pk">, null>> = {
subject: null,
body: null,
sender: null,
Expand Down
2 changes: 1 addition & 1 deletion src/shared/model/database/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface Folder extends Mutable<Model.Folder> {
rootConversationNodes: RootConversationNode[];
}

export interface Mail extends Omit<Model.Mail, "raw" | "body" | "attachments"> {
export interface Mail extends Skip<Model.Mail, "raw" | "body" | "attachments"> {
folders: Folder[];
score?: number;
}
Expand Down
2 changes: 1 addition & 1 deletion src/web/browser-window/app/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ interface GenericWebAccount<C extends AccountConfig, NS extends Notifications> {
loggedInOnce?: boolean;
loginDelayedSeconds?: number;
loginDelayedUntilSelected?: boolean;
fetchSingleMailParams: Omit<Arguments<CommonWebViewApi<AccountType>["fetchSingleMail"]>[0], keyof ZoneApiParameter> | null;
fetchSingleMailParams: Skip<Arguments<CommonWebViewApi<AccountType>["fetchSingleMail"]>[0], keyof ZoneApiParameter> | null;
}

export type WebAccountProtonmail = GenericWebAccount<AccountConfigProtonmail, NotificationsProtonmail>;
Expand Down
2 changes: 1 addition & 1 deletion src/web/browser-window/app/store/actions/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const ACCOUNTS_ACTIONS = unionize({
TryToLogin: ofType<{ account: WebAccount; webView: Electron.WebviewTag; }>(),
WireUpConfigs: ofType<{ accountConfigs: AccountConfig[] }>(),
PatchGlobalProgress: ofType<{ patch: State["globalProgress"]; }>(),
SelectMailOnline: ofType<Omit<Arguments<CommonWebViewApi<AccountType>["selectMailOnline"]>[0], "zoneName">>(),
SelectMailOnline: ofType<Skip<Arguments<CommonWebViewApi<AccountType>["selectMailOnline"]>[0], "zoneName">>(),
SetFetchSingleMailParams: ofType<{ pk: DbAccountPk }
& Partial<Pick<Exclude<WebAccount["fetchSingleMailParams"], null>, "mailPk">>>(),
FetchSingleMail: ofType<{ account: WebAccount; webView: Electron.WebviewTag; }
Expand Down
2 changes: 1 addition & 1 deletion src/web/browser-window/app/store/reducers/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export type ProgressPatch = Partial<{

type OptionalProps = "keytarSupport" | "electronLocations" | "checkUpdateAndNotify";

export interface State extends fromRoot.State, Partial<Pick<InitResponse, OptionalProps>>, Omit<InitResponse, OptionalProps> {
export interface State extends fromRoot.State, Partial<Pick<InitResponse, OptionalProps>>, Skip<InitResponse, OptionalProps> {
config: Config;
settings: Settings;
progress: ProgressPatch;
Expand Down
8 changes: 4 additions & 4 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"extends": [
"./node_modules/tslint/lib/configs/recommended",
"./tslint-extending/tslint-eslint-rules.json",
"./tslint-extending/codelyzer.json",
"./tslint-extending/tslint-rules-bunch.json",
"./tslint-extending/tslint-consistent-codestyle.json"
"./linting-extending/tslint/codelyzer.json",
"./linting-extending/tslint/tslint-consistent-codestyle.json",
"./linting-extending/tslint/tslint-eslint-rules.json",
"./linting-extending/tslint/tslint-rules-bunch.json"
],
"rules": {
"no-unused-variable": false,
Expand Down
Loading

0 comments on commit 21d5f1e

Please sign in to comment.