I am facing a challenge with the file test.json
that I want to import 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 code snippet is from my App.tsx
:
import { TestData } from './Types';
import * as testData from './test.json'
const ttt: TestData = testData;
This relates to Types.ts
export interface TestData {
items: TestDataPiece[]
}
export interface TestDataPiece {
kind: string,
thumbnails: {[key: string]: {
url: string,
width: number,
height: number
}}
}
In the tsconfig.json
, I have set
"resolveJsonModule": true
. However, an error occurred:
[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 ...