Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create a type in typescript that represents all strings except for a set of string literals?

I would like to use typescript overloading to provide intellisense for a function I'm writing. The signatures only differ by parameter type. The problem I'm having is that if someone passes in something that matches param1 in the first overload, but a param2 that doesn't, then it will fall through to the generic overload. I would like that to not be accepted and have intellisense mark it as an error.

I've tried using conditional typing using Exclude<> which didn't work.

type Exclusion = "Invalid";
type ExcludeString = Exclude<string, Exclusion>;

func(param1: "Activate", param2: (value: SomeInterface) => any): void;
func(param1: ExcludeString, param2: (value: any) => any): void;

When I call func("Activate", (value: NotSomeInterface) => console.log("Invalid")); I would like intellisense to tell the user there's an error. Instead, nothing happens due to the fall through.

like image 866
JeffGeoff Avatar asked Feb 01 '19 21:02

JeffGeoff


People also ask

How do you define a string type in TypeScript?

It is a type of primitive data type that is used to store text data. The string values are used between single quotation marks or double quotation marks, and also array of characters works same as a string. TypeScript string work with the series of character. var var_name = new String(string);

Are strings reference types in TypeScript?

string is a special reference type that acts like a value type in equality operator. When a string is passed to a function, its reference is passed to the function not a copy of its value.

Should you type everything in TypeScript?

Yes, you should make it a habit to explicitly set all types, it's will prevent having unexpected values and also good for readability.

What represent not having any data type in TypeScript?

Object. object is a type that represents the non-primitive type, i.e. anything that is not number , string , boolean , bigint , symbol , null , or undefined .


1 Answers

You can't define the type directly, Exclude<string, Exclusion> will actually just result in string typescript does not really have the concept of a negated type. There is a proposal to allow something similar (Here) but it's not yet part of the language (at the time of writing 3.3 is the latest version).

All this being said, we can get a similar effect if we are talking about a function parameter that should not be of a specific value. To do this we need a generic type parameter to capture the actual type of the string passed in. Then we can type the parameter as Exclude<T, Exclusion> and this will yield the expected errors:

type Exclusion = "Invalid";
type ExcludeString = Exclude<string, Exclusion>;

function func<T extends string>(param1: Exclude<T, Exclusion>): void {

} 

func("SS")
func("Invalid") // error
like image 142
Titian Cernicova-Dragomir Avatar answered Nov 15 '22 04:11

Titian Cernicova-Dragomir