Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

292
Visualizações
quicktype-core: generating TypeScript interfaces that allow extra unknown properties when parsing, without throwing an exception

I'm using NPM package: https://www.npmjs.com/package/quicktype-core to generate TypeScript interfaces from JSON samples.

Here's a simple example of the code that generates the interfaces:

const sample_object = {
    prop_a: 'value A',
    prop_b: 'value B',
};

const jsonInput = jsonInputForTargetLanguage('typescript');
await jsonInput.addSource({
    name: 'MyNewInterface',
    samples: [JSON.stringify(sample_object)],
});
const inputData = new InputData();
inputData.addInput(jsonInput);

const result = await quicktype({
    inputData,
    lang: 'typescript',
    inferDateTimes: false,
    alphabetizeProperties: true,
    inferEnums: false,
    rendererOptions: {},
});

// Display generated interface:
const generated_code = result.lines.join('\n');
console.log(generated_code);

The interface that it generates works when parsing new data that ONLY contains the properties it knows about. But if you try to parse some data that contains any extra fields/properties, it throws an exception, e.g. sample data:

const sample_object = {
    prop_a: 'value A',
    prop_b: 'value B',
    new_extra_property: 'value for new_extra_property',
};

Will throw exception:

Invalid value "value for new_extra_property" for type false

How can I configure quicktype-core to generate code that will NOT throw exceptions when data contains extra unknown fields?

I've tried trawling through the quicktype source code, and came across a couple of rendererOptions called additionalProperties + runtimeTypecheckIgnoreUnknownProperties ... but trying to set those as either true or a string doesn't make any difference at all to the code it generates.

Setting to true (actually gives TypeScript errors for the code, even though many quicktype-option renderedOptions are actually bools, and they've worked for other settings):

const result = await quicktype({
    inputData,
    lang: 'typescript',
    inferDateTimes: false,
    alphabetizeProperties: true,
    inferEnums: false,
    rendererOptions: {
        additionalProperties: true,
        runtimeTypecheckIgnoreUnknownProperties: true,
    },
});

Setting a string (keeps the TypeScript checking happy):

const result = await quicktype({
    inputData,
    lang: 'typescript',
    inferDateTimes: false,
    alphabetizeProperties: true,
    inferEnums: false,
    rendererOptions: {
        additionalProperties: 'string',
        runtimeTypecheckIgnoreUnknownProperties: 'string',
    },
});
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda