// data.json
{
"foo": ["bar"]
}
// main.ts
import type {ReadonlyDeep} from 'type-fest';
import dataJson = require('./data.json');
const data: ReadonlyDeep<typeof dataJson> = dataJson;
export default data;
// test.ts
import data from './main';
data.foo.push('bar');
//=> error TS2339: Property 'push' does not exist on type 'readonly string[]'
Note that types containing overloaded functions are not made deeply readonly due to a TypeScript limitation.
Convert
objects,Maps,Sets, andArrays and all of their keys/elements into immutable structures recursively.This is useful when a deeply nested structure needs to be exposed as completely immutable, for example, an imported JSON module or when receiving an API response that is passed around.
Please upvote this issue if you want to have this type as a built-in in TypeScript.