I have the following file test.json that I wish to import it in a typed form.
{
"items": [
{
"kind": "youtube#video",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/WnVAkK876rA/default.jpg",
"width": 120,
"height": 90
}
}
},
{
"kind": "youtube#video",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/jhTpc7nFtfI/default.jpg",
"width": 120,
"height": 90
},
"maxres": {
"url": "https://i.ytimg.com/vi/jhTpc7nFtfI/maxresdefault.jpg",
"width": 1280,
"height": 720
}
}
}
]
}
This is my App.tsx:
import { TestData } from './Types';
import * as testData from './test.json'
const ttt: TestData = testData;
This is Types.ts
export interface TestData {
items: TestDataPiece[]
}
export interface TestDataPiece {
kind: string,
thumbnails: {[key: string]: {
url: string,
width: number,
height: number
}}
}
In my tsconfig.json I use "resolveJsonModule": true. However I get this error:
[tsl] ERROR in C:\Users\home\IdeaProjects\yt-glasat\src\App.tsx(11,7)
TS2322: Type '{ items: ({ kind: string; thumbnails: { default: { url: string; width: number; height: number; }; maxres?: undefined; }; } | { kind: string; thumbnails: { default: { url: string; width: number; height: number; }; maxres: { ...; }; }; })[]; }' is not assignable to type 'TestData'.
Types of property 'items' are incompatible.
Type '({ kind: string; thumbnails: { default: { url: string; width: number; height: number; }; maxres?: undefined; }; } | { kind: string; thumbnails: { default: { url: string; width: number; height: number; }; maxres: { ...; }; }; })[]' is not assignable to type 'TestDataPiece[]'.
Type '{ kind: string; thumbnails: { default: { url: string; width: number; height: number; }; maxres?: undefined; }; } | { kind: string; thumbnails: { default: { url: string; width: number; height: number; }; maxres: { ...; }; }; }' is not assignable to type 'TestDataPiece'.
Type '{ kind: string; thumbnails: { default: { url: string; width: number; height: number; }; maxres?: undefined; }; }' is not assignable to type 'TestDataPiece'.
Types of property 'thumbnails' are incompatible.
Type '{ default: { url: string; width: number; height: number; }; maxres?: undefined; }' is not assignable to type '{ [key: string]: { url: string; width: number; height: number; }; }'.
Property '"maxres"' is incompatible with index signature.
Type 'undefined' is not assignable to type '{ url: string; width: number; height: number; }'.