How to force two types to be incompatible in TypeScript?

Viewed 292

I want to create two types in TypeScript, both of them are a number:

export type GlobalPortIndex = number;
export type TypePortIndex = number;

As far as I understand, TypeScript will consider these mutually compatible since they overlap perfectly.

But I want them to be incompatible, so when I type something GlobalPortIndex, then if I try to assign a TypePortIndex to it, I'd get a type error. This is to make sure I don't mix them up in the code, for example I can't pass one to a function that expects the other.

Any TypeScript tricks to achieve this?

3 Answers

I am no expert on typescript, but wouldn't it be better if you actually wrap the value into class and then static check class type?

Since it looks like you have a object type as GlobalPortIndex, and the other one is TypePortIndex, they serve a different purpose.

Or perhaps set them as enum syntax can resolve this issue?

enum FooKeys {
  FOO = 'foo',
  BAR = 'bar',
}

// probably all you need, but it's a type alias
type FooType = Record<FooKeys, string>;

The code above is referenced from this post: How to build a type from enum values in TypeScript?

You need to declare additional property (or method) to make the 2 classes different:

class GlobalPointerIndex extends Number {
  __GlobalPointerIndex: boolean;
}

class TypePortIndex extends Number {
  __TypePortIndex: boolean;
}

try this:

export interface GlobalPortIndex {
brand : 'GlobalPortIndex';
value : number;
}

export interface TypePortIndex {
brand : 'TypePortIndex';
value : number;
}

but you have to use it like :

const myport = {value:25} as TypePortIndex

and access to value like myport.value

Related