Skip to content

Commit

Permalink
INT-3308: Overriden props are now typed as an internal DoNotUse type (
Browse files Browse the repository at this point in the history
#527)

* INT-3308: Overriden props are now typed as an internal `DoNotUse` type

* INT-3308: Added a test
  • Loading branch information
danoaky-tiny authored May 20, 2024
1 parent 1cd38cf commit d822088
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/main/ts/components/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,22 @@ import { isFunction, isTextareaOrInput, mergePlugins, uuid, configHandlers, isBe
import { EditorPropTypes, IEditorPropTypes } from './EditorPropTypes';
import { Bookmark, Editor as TinyMCEEditor, EditorEvent, TinyMCE } from 'tinymce';

type EditorOptions = Parameters<TinyMCE['init']>[0];
type OmitStringIndexSignature<T> = { [K in keyof T as string extends K ? never : K]: T[K] };

interface DoNotUse<T extends string> {
__brand: T;
}

type OmittedInitProps = 'selector' | 'target' | 'readonly' | 'license_key';

export type EditorOptions = Parameters<TinyMCE['init']>[0];

export type InitOptions = Omit<OmitStringIndexSignature<EditorOptions>, OmittedInitProps> & {
selector?: DoNotUse<'selector prop is handled internally by the component'>;
target?: DoNotUse<'target prop is handled internally by the component'>;
readonly?: DoNotUse<'readonly prop is overridden by the component, use the `disabled` prop instead'>;
license_key?: DoNotUse<'license_key prop is overridden by the integration, use the `licenseKey` prop instead'>;
};

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

Expand All @@ -17,7 +32,7 @@ export interface IProps {
initialValue: string;
onEditorChange: (a: string, editor: TinyMCEEditor) => void;
value: string;
init: EditorOptions & Partial<Record<'selector' | 'target' | 'readonly' | 'license_key', undefined>>;
init: InitOptions;
tagName: string;
tabIndex: number;
cloudChannel: Version;
Expand Down Expand Up @@ -348,7 +363,7 @@ export class Editor extends React.Component<IAllProps> {
}

const finalInit: EditorOptions = {
...this.props.init,
...this.props.init as Omit<InitOptions, OmittedInitProps>,
selector: undefined,
target,
readonly: this.props.disabled,
Expand Down
10 changes: 10 additions & 0 deletions src/test/ts/browser/EditorInitTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ describe('EditorInitTest', () => {
ctx.reRender({ ...defaultProps, disabled: true });
Assertions.assertEq('Should be readonly mode', true, '4' === version ? ctx.editor.readonly : ctx.editor.mode.get() === 'readonly');
});

it('Using an overriden props will cause a TS error', async () => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
using _ = await render({
init: {
// @ts-expect-error Overriden props
target: document.createElement('div'), readonly: true, selector: 'textarea#my-id', license_key: 'gpl'
}
});
});
})
);
});

0 comments on commit d822088

Please sign in to comment.