How can I say that I want the interface to be one way or another, but not both, or in any way?
interface IFoo { bar: string /*^XOR^*/ can: number; }
To achieve this, you can use union types together with never:
never
type IFoo = { bar: string; can?: never } | { bar?: never; can: number }; let val0: IFoo = { bar: "hello" } // OK only bar let val1: IFoo = { can: 22 } // OK only can let val2: IFoo = { bar: "hello", can: 22 } // Error foo and can let val3: IFoo = { } // Error neither foo or can
You can get one and not the other with a union and optional void type :
type IFoo = {bar: string; can?: void} | {bar?:void; can: number};
However, you need to use it --strictNullChecksin order to have neither one nor the other.
--strictNullChecks
:
type Foo = { bar?: void; foo: string; } type Bar = { foo?: void; bar: number; } type FooBar = Foo | Bar; // Error: Type 'string' is not assignable to type 'void' let foobar: FooBar = { foo: "1", bar: 1 } // no errors let foo = { foo: "1" }
Source: https://habr.com/ru/post/1678749/More articles:Сборник Visual Studio Team Services не может найти библиотеку - tfsiOS11 Core NFC and ISO 14443 - iosStructure and Organization of Airflow Dags and Tasks - airflowUsing NSKeyValueObservation to Observe a Value in UserDefaults - iosHow to call a function but not receive a response until there is a value? - javascriptHow to choose a different type of shell in the integrated Visual Studio Code terminal - shellWhy does installing one npm package remove many others? - npmmget cannot get a primitive function, for example get - rWhat are the required options for downloading an excel file? - scalahttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1678754/add-tags-to-talk-for-many-to-many-relationship-in-entity-framework&usg=ALkJrhggBggMkULrbBXEanGq0VDbM9ZscgAll Articles