Background:
I'm trying to get typescript to scream when trying to pass an object that has a certain field, but can't make it work for this particular case.
An example code:
import * as React from "react";
// type ReactIntl.MessageValue = string | number | boolean | Date | null | undefined
export type IValues = {
[key: string]: JSX.Element | ReactIntl.MessageValue;
} | undefined;
export interface INewValues extends Omit<IValues, 'date'> { }
const Foo = () => {
const values: INewValues = { date: "25/10/2030" }; // No error
console.log(values);
return null;
}
Rendering the component will log the following into console:
>> {date: '25/10/2030'}
My question:
Is it possible to tell Typescript that assigning an object with a 'date' field into a variable of type "INewValues" is forbidden?
My goal is to create a type named "INewValues" that has everything "IValues" has, but without a 'date' field.
"IValues" is the type of react-intl
's FormattedMessage
component's values
prop. So basically what I'm trying to do is to forbid developers from passing a values
object with a 'date' field.
Note:
When destructuring 'date' from values it does gives the following error as expected:
"Property 'date' does not exist on type 'INewValues'."
const values: INewValues = { date: "25/10/2030" }; // No error
const { date } = values; // Error
console.log(date);
>> 25/10/2030