Nominal type in typescript

Cheetah

I have the following code:

interface A {}
interface B {}

let a: A;
let b: B;

// I want this to fail to compile
a = b;

Because interfaces A and B look the same it compiles just fine. How can I define variable a such that a = b fails to compile because the types are different?

Typescript 4.7.4.

Robert Rendell

These interfaces (shapes) overlap, so even though a: A, it can still be assigned to b: B.

interface A {
    type: 'A'
}
interface B {
    type: 'B'
}

let a: A;
let b: B;

// fails
a = b;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Nominal type roles and data families

Using Nominal Roles for Type Inference

How do you emulate nominal typing in TypeScript?

What is a "nominal type" in the context of an inherent implementation?

How to type check nominal-typed IDs

Is there a way to create nominal types in TypeScript that extend primitive types?

non-nominal type X does not support explicit initialization

Simplest examples demonstrating the need for nominal type role in Haskell

Non-nominal type 'User' does not support explicit initialization

While working in Weka do the last attribute data type need to be nominal?

can we auto detect the feature type as nominal or ordinal in pandas?

nominal to bi-nominal in python

Non-nominal type "Self" doesn't support explicit initialisation (protocol extension for enums)

Swift 4: "Non-nominal type 'T' does not support explicit initialization"

Typescript type a or type b

What is the type of a type in Typescript

Referencing type of a type in Typescript

error: Couldn't lookup symbols: nominal type descriptor for Swift.Task.Handle trying async/await with Xcode 12.5 beta / Swift 5.5

Swift 4: Non-nominal type 'T' does not support explicit initialization when converting Objective-c to Swift

Typescript - Type '[][]' is not assignable to type '[]' error

Typescript Type 'string' is not assignable to type

Defining class type in type typescript

React with TypeScript: Type '{}' is not assignable to type

Typescript: Type 'string' is not assignable to type

Is there a type for "any enum type" in TypeScript?

Array<Type> VS Type[] in Typescript

Typescript type a is not assignable to type b

Difference between type[] and [type] in typescript

TypeScript: tuple type to object type

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  6. 6

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  7. 7

    Do Idle Snowflake Connections Use Cloud Services Credits?

  8. 8

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  9. 9

    Binding element 'string' implicitly has an 'any' type

  10. 10

    BigQuery - concatenate ignoring NULL

  11. 11

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  12. 12

    In Skype, how to block "User requests your details"?

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    Pandas - check if dataframe has negative value in any column

  15. 15

    flutter: dropdown item programmatically unselect problem

  16. 16

    Generate random UUIDv4 with Elm

  17. 17

    Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

  18. 18

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  19. 19

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  20. 20

    EXCEL: Find sum of values in one column with criteria from other column

  21. 21

    How to use merge windows unallocated space into Ubuntu using GParted?

HotTag

Archive