score:2
Accepted answer
You did not explicitly define a type for s
, so the compiler tries to infer one, based on the provided values.
The inferred type of s
is:
{
name: undefined;
age: undefined;
}
You have set an explicit type like so:
type User = {
name: string | undefined;
age: number | undefined;
};
const s: User = {
name: undefined,
age: undefined
};
s.name = "Test"; //should work
score:1
If you look at the inferred type of s
you can see that it is
{
name:undefined;
age:undefined;
}
TypeScript can't infer that any other type might be allowed here because you didn't supply that info.
Create a type:
interface Person{
name: string | undefined;
age: number | undefined;
}
then declare s
with this type:
const s:Person = {
name: undefined;
age: undefined;
}
Source: stackoverflow.com
Related Query
- Typescript giving Type '"Test"' is not assignable to type 'undefined'
- How to fix the error argument of type string or undefined is not assignable to parameter of type string using typescript and react?
- TypeScript Error (TS2345) argument of type Person | undefined is not assignable to paramater of type Person
- How to fix error argument of type undefined is not assignable to parameter of type 'string or () => string usng typescript and react?
- useRef TypeScript - not assignable to type LegacyRef<HTMLDivElement>
- React Typescript - Argument of type is not assignable to parameter of type
- Typescript React/Redux : Argument of type 'typeof MyClass' is not assignable to parameter of type 'ComponentType<...'
- Using typescript in react,stateless component not assignable to type 'React.SFC'
- react-router-dom TypeScript TS2322: Type 'typeof Index' is not assignable to type
- React Router 6 and Typescript - index attribute Type 'true' is not assignable to type 'false | undefined'.?
- React with TypeScript - Type {...} is not assignable to type 'Readonly<S>'
- TypeScript error: Type '() => boolean' is not assignable to type 'boolean'
- CombineReducers with Typescript returns error "Argument of type is not assignable to parameter of type 'ReducersMapObject'"
- Type 'void' is not assignable to type '((event: ChangeEvent<HTMLInputElement>) => void) React TypeScript
- React Typescript error in TextArea : Type 'string' is not assignable to type number
- Typescript error: Type 'string' is not assignable to type '"allName" | `allName.${number}.nestedArray`' in react hook form
- Creating reusable Button Component with React and Typescript with not assignable type error
- React js Typescript Argument of type 'string' is not assignable to parameter of type 'SetStateAction<number>'
- TypeScript + useRef<X|Y>: Type X is not assignable to type Y
- React & TypeScript HOCs - why I get Type '{}' is not assignable to Type P?
- TypeScript React Js, Type 'void' is not assignable to type '((event: MouseEvent<HTMLDivElement, MouseEvent>) => void) | undefined'
- useRef throws not assignable to type LegacyRef<Slider> | undefined
- Typescript error: Type '{ onClick: () => void; }' is not assignable to type 'IntrinsicAttributes'
- Typescript - string' is not assignable to type 'FC
- Typescript error 'argument of type is not assignable to parameter of type'
- TypeScript error: Type 'string' is not assignable to type for Typography
- TypeScript error : Type '.....' is not assignable to type 'IntrinsicAttributes & Number'
- TypeScript w/ React - Argument of type 'string' is not assignable to parameter of type 'keyof T'
- got Type 'void' is not assignable to type '() => void' in react typescript
- useEffect cleanup function Typescript error: not assignable to parameter of type 'EffectCallback'
More Query from same tag
- Reactjs - toggling class indipendently with right way
- React - ShowRainBow.state: must be set to an object or null
- How to put object in ternary conditional?
- Manage with React api query useState Hook
- In d3 line chart not able to see the month name if the date is last day of the month
- How can i make lib file on reactJS
- Typescript error on local machine but not on remote test server
- New Object Property in React
- Post route for register
- set origin coordinates for react-konva stage
- React hooks setState not updating immediately
- CSS Modules & ReactJS: Parent and child CSS classes in different components
- React-select scrollintoView does not scroll the selected value
- Is it a bad practice to use state in a React component if the data will not change? Should I use a property on the class instead?
- How can we show or hide components inside OverReact (library to build react components using dart ) according to conditions?
- Effect of Key in component extending React.Component
- Remove Item on click in React
- Hide/Show component using componentDidMount
- How to get object values as type in typescript?
- What is the TypeScript return type of a React stateless component?
- How i can use DropDown from Materialize css in React
- Dynamic content inside Trans Component i18next
- getting error while destructuring the data from react redux. why it is giving error and how to solve this?
- bufferGeometry and shadow react-three-fiber
- queryCache.find() is undefined
- test if only 1 radiobutton is selected in a radiogroup component after fireEvent.click in react-testing-library
- How to go back to previous page using back button/Link click using react-router-dom
- React Javascript displaying/decoding unicode characters
- How to map through nested array of objects in React
- Preventing input submit from refreshing page