josdejong / svelte-jsoneditor

A web-based tool to view, edit, format, repair, query, transform, and validate JSON
https://jsoneditoronline.org
Other
820 stars 108 forks source link

type error while defining mode #244

Closed gurmeet-bluesheets closed 1 year ago

gurmeet-bluesheets commented 1 year ago

I am using this in a svelte file as follows

<script lang="ts">
    import {
        JSONEditor,
        type ContentErrors,
        type JSONPatchResult,
        type Content
    } from 'svelte-jsoneditor';

    export let dataToPrefill: NodeType = {};

    let credSchema: Content = {
        text: undefined, // can be used to pass a stringified JSON document instead
        json: dataToPrefill?.cred_schema || {}
    };

    function addNode(event: Event) {
        const formData = fetchFormData(event);
        formData.cred_schema = 'text' in credSchema ? credSchema.text : JSON.stringify(credSchema.json);
        formData.error_messages = '{}';

        if (!('http_method' in formData)) {
            formData.http_method = '';
        }
        CreateNodeAction(formData, hideAddEditNodeDrawer);
    }

    function handleCredSchemaChange(
        updatedContent: Content,
        previousContent: Content,
        changeStatus: {
            contentErrors: ContentErrors | null;
            patchResult: JSONPatchResult | null;
        }
    ) {
        credSchema = updatedContent;
    }

</script>

<div>
           <JSONEditor
            content={credSchema}
            onChange={handleCredSchemaChange}
            readOnly={isViewOnlyMode}
            mode="text"
        />
</div>

I am getting typescript error in line where I wrote mode="text"

Error - Type '"text"' is not assignable to type 'Mode | undefined'.

Can someone help me in what I am doing wrong?

josdejong commented 1 year ago

The option mode is a TypeScript enum, you can use Mode.text

I wish TypeScript would understand that the string 'text' is valid too since it corresponds to one of the enum values 😅 .