Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

293
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda