Skip to content

Commit d822088

Browse files
authored
INT-3308: Overriden props are now typed as an internal DoNotUse type (#527)
* INT-3308: Overriden props are now typed as an internal `DoNotUse` type * INT-3308: Added a test
1 parent 1cd38cf commit d822088

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/main/ts/components/Editor.tsx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,22 @@ import { isFunction, isTextareaOrInput, mergePlugins, uuid, configHandlers, isBe
66
import { EditorPropTypes, IEditorPropTypes } from './EditorPropTypes';
77
import { Bookmark, Editor as TinyMCEEditor, EditorEvent, TinyMCE } from 'tinymce';
88

9-
type EditorOptions = Parameters<TinyMCE['init']>[0];
9+
type OmitStringIndexSignature<T> = { [K in keyof T as string extends K ? never : K]: T[K] };
10+
11+
interface DoNotUse<T extends string> {
12+
__brand: T;
13+
}
14+
15+
type OmittedInitProps = 'selector' | 'target' | 'readonly' | 'license_key';
16+
17+
export type EditorOptions = Parameters<TinyMCE['init']>[0];
18+
19+
export type InitOptions = Omit<OmitStringIndexSignature<EditorOptions>, OmittedInitProps> & {
20+
selector?: DoNotUse<'selector prop is handled internally by the component'>;
21+
target?: DoNotUse<'target prop is handled internally by the component'>;
22+
readonly?: DoNotUse<'readonly prop is overridden by the component, use the `disabled` prop instead'>;
23+
license_key?: DoNotUse<'license_key prop is overridden by the integration, use the `licenseKey` prop instead'>;
24+
};
1025

1126
export type Version = `${'4' | '5' | '6' | '7'}${'' | '-dev' | '-testing' | `.${number}` | `.${number}.${number}`}`;
1227

@@ -17,7 +32,7 @@ export interface IProps {
1732
initialValue: string;
1833
onEditorChange: (a: string, editor: TinyMCEEditor) => void;
1934
value: string;
20-
init: EditorOptions & Partial<Record<'selector' | 'target' | 'readonly' | 'license_key', undefined>>;
35+
init: InitOptions;
2136
tagName: string;
2237
tabIndex: number;
2338
cloudChannel: Version;
@@ -348,7 +363,7 @@ export class Editor extends React.Component<IAllProps> {
348363
}
349364

350365
const finalInit: EditorOptions = {
351-
...this.props.init,
366+
...this.props.init as Omit<InitOptions, OmittedInitProps>,
352367
selector: undefined,
353368
target,
354369
readonly: this.props.disabled,

src/test/ts/browser/EditorInitTest.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ describe('EditorInitTest', () => {
7575
ctx.reRender({ ...defaultProps, disabled: true });
7676
Assertions.assertEq('Should be readonly mode', true, '4' === version ? ctx.editor.readonly : ctx.editor.mode.get() === 'readonly');
7777
});
78+
79+
it('Using an overriden props will cause a TS error', async () => {
80+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
81+
using _ = await render({
82+
init: {
83+
// @ts-expect-error Overriden props
84+
target: document.createElement('div'), readonly: true, selector: 'textarea#my-id', license_key: 'gpl'
85+
}
86+
});
87+
});
7888
})
7989
);
8090
});

0 commit comments

Comments
 (0)