From fb72bd997534e96c58bcad8aeb6ddc654b5f091e Mon Sep 17 00:00:00 2001 From: Ben Grynhaus Date: Mon, 15 Oct 2018 23:41:31 +0300 Subject: [PATCH 1/7] Added KnownKeys mapped type --- .../core/src/lib/declarations/known-keys.d.ts | 22 +++++++++++++++++++ .../core/src/lib/declarations/public-api.d.ts | 1 + 2 files changed, 23 insertions(+) create mode 100644 libs/core/src/lib/declarations/known-keys.d.ts diff --git a/libs/core/src/lib/declarations/known-keys.d.ts b/libs/core/src/lib/declarations/known-keys.d.ts new file mode 100644 index 00000000..ced16ee5 --- /dev/null +++ b/libs/core/src/lib/declarations/known-keys.d.ts @@ -0,0 +1,22 @@ +/** + * Get the known keys (i.e. no index signature) of T. + * + * @example +```typescript +interface Options { + key: string; + title: string; + [dataProperty: string]: string | number; +} + +type KnownKeysOfOptions = KnownKeys; // 'key' | 'title'; +// (vs. type KeysOfOptions = keyof Options // string | number;) +``` + + * Taken from https://stackoverflow.com/questions/51465182/typescript-remove-index-signature-using-mapped-types + */ +export type KnownKeys = { [K in keyof T]: string extends K ? never : number extends K ? never : K } extends { + [_ in keyof T]: infer U +} + ? U + : never; diff --git a/libs/core/src/lib/declarations/public-api.d.ts b/libs/core/src/lib/declarations/public-api.d.ts index 887206a2..d0a04936 100644 --- a/libs/core/src/lib/declarations/public-api.d.ts +++ b/libs/core/src/lib/declarations/public-api.d.ts @@ -1,5 +1,6 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +export * from './known-keys'; export * from './omit'; export * from './StringMap'; From 4f039adcd300748fdcf6e7b4f53de82b9c00a138 Mon Sep 17 00:00:00 2001 From: Ben Grynhaus Date: Mon, 15 Oct 2018 23:41:54 +0300 Subject: [PATCH 2/7] Add IndexSignature mapped type --- .../src/lib/declarations/index-signature.d.ts | 15 +++++++++++++++ libs/core/src/lib/declarations/public-api.d.ts | 1 + 2 files changed, 16 insertions(+) create mode 100644 libs/core/src/lib/declarations/index-signature.d.ts diff --git a/libs/core/src/lib/declarations/index-signature.d.ts b/libs/core/src/lib/declarations/index-signature.d.ts new file mode 100644 index 00000000..669469dd --- /dev/null +++ b/libs/core/src/lib/declarations/index-signature.d.ts @@ -0,0 +1,15 @@ +/** + * Gets the index signature of a type. + * + * @example + ```typescript +interface Options { + key: string; + title: string; + [dataProperty: string]: string | number; +} + +type IndexOfOptions = IndexSignature; // { [x: string]: string | number; [x: number]: string | number; } +``` + */ +export type IndexSignature = Pick; diff --git a/libs/core/src/lib/declarations/public-api.d.ts b/libs/core/src/lib/declarations/public-api.d.ts index d0a04936..11137158 100644 --- a/libs/core/src/lib/declarations/public-api.d.ts +++ b/libs/core/src/lib/declarations/public-api.d.ts @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +export * from './index-signature'; export * from './known-keys'; export * from './omit'; export * from './StringMap'; From ba64b4f4bbe712de51b675522b86bf14bc7f42f9 Mon Sep 17 00:00:00 2001 From: Ben Grynhaus Date: Mon, 15 Oct 2018 23:42:42 +0300 Subject: [PATCH 3/7] Better Omit type --- libs/core/src/lib/declarations/omit.d.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libs/core/src/lib/declarations/omit.d.ts b/libs/core/src/lib/declarations/omit.d.ts index 8a7a8923..72c08794 100644 --- a/libs/core/src/lib/declarations/omit.d.ts +++ b/libs/core/src/lib/declarations/omit.d.ts @@ -1,4 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -export type Omit = Pick>; +import { IndexSignature } from './index-signature'; +import { KnownKeys } from './known-keys'; + +export type Omit = Pick & keyof T, K>> & IndexSignature; From 56cbbfc2ed77e1accd8c71099604820b25d33bad Mon Sep 17 00:00:00 2001 From: Ben Grynhaus Date: Mon, 15 Oct 2018 23:43:01 +0300 Subject: [PATCH 4/7] Better type for ICommandBarItemOptions. Now completes in intellisense/auto-complete properly. --- .../command-bar/command-bar.component.ts | 31 +++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/libs/fabric/src/lib/components/command-bar/command-bar.component.ts b/libs/fabric/src/lib/components/command-bar/command-bar.component.ts index bcef9d15..d3d276c9 100644 --- a/libs/fabric/src/lib/components/command-bar/command-bar.component.ts +++ b/libs/fabric/src/lib/components/command-bar/command-bar.component.ts @@ -2,7 +2,21 @@ // Licensed under the MIT License. import { InputRendererOptions, Omit, ReactWrapperComponent } from '@angular-react/core'; -import { AfterContentInit, ChangeDetectionStrategy, ChangeDetectorRef, Component, ContentChild, ElementRef, EventEmitter, Input, OnDestroy, Output, QueryList, Renderer2, ViewChild } from '@angular/core'; +import { + AfterContentInit, + ChangeDetectionStrategy, + ChangeDetectorRef, + Component, + ContentChild, + ElementRef, + EventEmitter, + Input, + OnDestroy, + Output, + QueryList, + Renderer2, + ViewChild, +} from '@angular/core'; import { ICommandBarItemProps, ICommandBarProps } from 'office-ui-fabric-react/lib/CommandBar'; import { IContextualMenuItem } from 'office-ui-fabric-react/lib/ContextualMenu'; import { Subscription } from 'rxjs'; @@ -10,7 +24,12 @@ import { OnChanges, TypedChanges } from '../../declarations/angular/typed-change import omit from '../../utils/omit'; import { mergeItemChanges } from '../core/declarative/item-changed'; import { CommandBarItemChangedPayload, CommandBarItemDirective } from './directives/command-bar-item.directives'; -import { CommandBarFarItemsDirective, CommandBarItemsDirective, CommandBarItemsDirectiveBase, CommandBarOverflowItemsDirective } from './directives/command-bar-items.directives'; +import { + CommandBarFarItemsDirective, + CommandBarItemsDirective, + CommandBarItemsDirectiveBase, + CommandBarOverflowItemsDirective, +} from './directives/command-bar-items.directives'; @Component({ selector: 'fab-command-bar', @@ -195,9 +214,10 @@ export class FabCommandBarComponent extends ReactWrapperComponent iconRenderer({ contextualMenuItem: item }), - } as any /* NOTE: Fix for wrong typings of `onRenderIcon` in office-ui-fabric-react */, + iconRenderer && + ({ + onRenderIcon: (item: IContextualMenuItem) => iconRenderer({ contextualMenuItem: item }), + } as any) /* NOTE: Fix for wrong typings of `onRenderIcon` in office-ui-fabric-react */, renderer && ({ onRender: (item, dismissMenu) => renderer({ item, dismissMenu }) } as Pick) ) as ICommandBarItemProps; @@ -205,7 +225,6 @@ export class FabCommandBarComponent extends ReactWrapperComponent extends Omit { - readonly [propertyName: string]: any; readonly renderIcon?: InputRendererOptions; readonly render?: InputRendererOptions; readonly data?: TData; From 8c61c524cd4322523a5e9f2e0406116054dfdd5a Mon Sep 17 00:00:00 2001 From: Ben Grynhaus Date: Tue, 16 Oct 2018 00:03:17 +0300 Subject: [PATCH 5/7] Fix Omit type to work on types that don't have an index signature --- libs/core/src/lib/declarations/omit.d.ts | 6 +++++- libs/core/src/lib/declarations/unknown-keys.d.ts | 5 +++++ 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 libs/core/src/lib/declarations/unknown-keys.d.ts diff --git a/libs/core/src/lib/declarations/omit.d.ts b/libs/core/src/lib/declarations/omit.d.ts index 72c08794..308d4658 100644 --- a/libs/core/src/lib/declarations/omit.d.ts +++ b/libs/core/src/lib/declarations/omit.d.ts @@ -3,5 +3,9 @@ import { IndexSignature } from './index-signature'; import { KnownKeys } from './known-keys'; +import { UnknownKeys } from './unknown-keys'; -export type Omit = Pick & keyof T, K>> & IndexSignature; +export type _KnownKeysOmit = Pick & keyof T, K>>; +export type Omit = UnknownKeys extends never + ? _KnownKeysOmit + : _KnownKeysOmit & IndexSignature; diff --git a/libs/core/src/lib/declarations/unknown-keys.d.ts b/libs/core/src/lib/declarations/unknown-keys.d.ts new file mode 100644 index 00000000..66ddf6a9 --- /dev/null +++ b/libs/core/src/lib/declarations/unknown-keys.d.ts @@ -0,0 +1,5 @@ +export type UnknownKeys = { [K in keyof T]: string extends K ? K : number extends K ? K : never } extends { + [_ in keyof T]: infer U +} + ? U + : never; From 2c774be3cd89d96f5f3b571122651a336c40992e Mon Sep 17 00:00:00 2001 From: Ben Grynhaus Date: Tue, 16 Oct 2018 00:23:19 +0300 Subject: [PATCH 6/7] Added `skipLibCheck: true` to root tsconfig.json. `office-ui-fabric-react` sometimes has mismatching types that cause issues at compile time that we can't fix. --- tsconfig.json | 1 + 1 file changed, 1 insertion(+) diff --git a/tsconfig.json b/tsconfig.json index 5d1aa609..c8ba5d27 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,6 +10,7 @@ "typeRoots": ["node_modules/@types"], "lib": ["es2017", "dom"], "baseUrl": ".", + "skipLibCheck": true, "paths": { "@angular-react/*": ["libs/*"], "@angular-react/core": ["libs/core/src/index.ts"], From e584058b0df97a43e2b075426a2c6ae330988d24 Mon Sep 17 00:00:00 2001 From: Ben Grynhaus Date: Tue, 16 Oct 2018 00:36:19 +0300 Subject: [PATCH 7/7] Remove types that didn't work well --- .../src/lib/declarations/index-signature.d.ts | 15 --------------- libs/core/src/lib/declarations/omit.d.ts | 7 +------ libs/core/src/lib/declarations/unknown-keys.d.ts | 5 ----- 3 files changed, 1 insertion(+), 26 deletions(-) delete mode 100644 libs/core/src/lib/declarations/index-signature.d.ts delete mode 100644 libs/core/src/lib/declarations/unknown-keys.d.ts diff --git a/libs/core/src/lib/declarations/index-signature.d.ts b/libs/core/src/lib/declarations/index-signature.d.ts deleted file mode 100644 index 669469dd..00000000 --- a/libs/core/src/lib/declarations/index-signature.d.ts +++ /dev/null @@ -1,15 +0,0 @@ -/** - * Gets the index signature of a type. - * - * @example - ```typescript -interface Options { - key: string; - title: string; - [dataProperty: string]: string | number; -} - -type IndexOfOptions = IndexSignature; // { [x: string]: string | number; [x: number]: string | number; } -``` - */ -export type IndexSignature = Pick; diff --git a/libs/core/src/lib/declarations/omit.d.ts b/libs/core/src/lib/declarations/omit.d.ts index 308d4658..1affee35 100644 --- a/libs/core/src/lib/declarations/omit.d.ts +++ b/libs/core/src/lib/declarations/omit.d.ts @@ -1,11 +1,6 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -import { IndexSignature } from './index-signature'; import { KnownKeys } from './known-keys'; -import { UnknownKeys } from './unknown-keys'; -export type _KnownKeysOmit = Pick & keyof T, K>>; -export type Omit = UnknownKeys extends never - ? _KnownKeysOmit - : _KnownKeysOmit & IndexSignature; +export type Omit = Pick & keyof T, K>>; diff --git a/libs/core/src/lib/declarations/unknown-keys.d.ts b/libs/core/src/lib/declarations/unknown-keys.d.ts deleted file mode 100644 index 66ddf6a9..00000000 --- a/libs/core/src/lib/declarations/unknown-keys.d.ts +++ /dev/null @@ -1,5 +0,0 @@ -export type UnknownKeys = { [K in keyof T]: string extends K ? K : number extends K ? K : never } extends { - [_ in keyof T]: infer U -} - ? U - : never;