error when creating an atom containing an object from a type: property does not exist in type 'Read<Type, never>' #3118
-
Bug Descriptioni'm creating an atom that contains an object that should follow a type but it's complaining about property 'x' not existing in type interface DisplayedSong {
song: number;
bookFirstIndex: number;
bookLastIndex: number;
currentReport?: any;
}
const displayedSongInfoAtom = atom<DisplayedSong>({
song: 1, // <- here it's giving an error
bookFirstIndex: undefined,
bookLastIndex: undefined,
currentReport: undefined,
});
interface MyObject {
first: number;
index: number;
}
const myObject = atom<MyObject>({
first: 1, // <- here it isn't
index: -1
}); the error is:
i have set strict and strictNullChecks to true in tsconfig.json i have provided a repro in the App.tsx file of the stackblitz project Reproduction Linkhttps://stackblitz.com/edit/vitejs-vite-qkuuzp9l?file=src%2FApp.tsx |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
It's because your initial value doesn't match with your type. import { atom } from 'jotai';
interface DisplayedSong {
song: number;
bookFirstIndex: number | undefined; // <- accept undefined
bookLastIndex: number | undefined;
currentReport?: any;
}
const displayedSongInfoAtom = atom<DisplayedSong>({
song: 1, // <- here it's giving an error
bookFirstIndex: undefined,
bookLastIndex: undefined,
currentReport: undefined,
});
interface MyObject {
first: number;
index: number;
}
const myObject = atom<MyObject>({
first: 1, // <- here it isn't
index: -1
}); I agree the TS error message isn't very understandable, but I'm not sure if it can be helped. |
Beta Was this translation helpful? Give feedback.
It's because your initial value doesn't match with your type.
This fixes it: https://tsplay.dev/NaM02N
I agree the TS error message isn't very understandable, but I'm not su…