Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring a single type - "using" equivalent for single type

Tags:

typescript

In C# I can do this:

using IAnyType = App.Namespace.Types.IAnyType ;
class BaseClass : IAnyType { }

Is there a Typescript equivalent?

//BAD:
import IDialogOptions = App.Widgets.Interfaces.IDialogOptions; //A module cannot be aliased as a non-module type
class BaseClass implements IDialogOptions { }

//BAD:     
declare var IDialogOptions: App.Widgets.Interfaces.IDialogOptions;
class BaseClass implements IDialogOptions { } //The name IDialogOptions does not exist in the current context   

The closest I can get is:

import Interfaces = App.Widgets.Interfaces;
class BaseDialog implements Interfaces.IDialogOptions { }

It's not ideal to use this long name every time I need to use this interface. I guess it's not all the bad but I was wondering if there's a better?

like image 915
parliament Avatar asked Jan 16 '13 01:01

parliament


People also ask

How do you declare a variable with two types TypeScript?

A union type allows us to define a variable with multiple types. The union type variables are defined using the pipe ( '|' ) symbol between the types. The union types help in some special situations. For example, when migrating from JavaScript code to TypeScript code.

What is ?: In TypeScript?

What does ?: mean in TypeScript? Using a question mark followed by a colon ( ?: ) means a property is optional. That said, a property can either have a value based on the type defined or its value can be undefined .

How do you declare a union type variable in TypeScript?

TypeScript 1.4 gives programs the ability to combine one or two types. Union types are a powerful way to express a value that can be one of the several types. Two or more data types are combined using the pipe symbol (|) to denote a Union Type.

How do I declare in TypeScript?

The type syntax for declaring a variable in TypeScript is to include a colon (:) after the variable name, followed by its type. Just as in JavaScript, we use the var keyword to declare a variable. Declare its type and value in one statement.

What is type conversion in Java with example?

Type conversion in Java with Examples. When you assign value of one data type to another, the two types might not be compatible with each other. If the data types are compatible, then Java will perform the conversion automatically known as Automatic Type Conversion and if not then they need to be casted or converted explicitly.

How many concrete types can be declared in a single Declaration?

Thus, this single declaration actually declares an unlimited number of types: Point {Float64}, Point {AbstractString}, Point {Int64}, etc. Each of these is now a usable concrete type:

What is it called when you write code with different types?

The ability to write code that can operate on different types is called polymorphism. All code in classic dynamically typed languages is polymorphic: only by explicitly checking types, or when objects fail to support operations at run-time, are the types of any values ever restricted.

What types are not subtypes of each other?

Other types, of course, are not subtypes of it: Concrete Point types with different values of T are never subtypes of each other: This last point is very important: even though Float64 <: Real we DO NOT have Point {Float64} <: Point {Real}.


2 Answers

I actually prefer your original solution over the alternatives:

import Interfaces = App.Widgets.Interfaces;
class BaseDialog implements Interfaces.IDialogOptions { }

Without having the alias (Interfaces) you could get naming collisions - which if you have ever had to handle with using statements is a real pain as you end up using an alias or using the full namespace.

using myAlias = App.Namespace.Types;

At least having an alias for all modules you import means this won't be an issue.

like image 121
Fenton Avatar answered Oct 06 '22 00:10

Fenton


I too am annoyed that you can't alias things the way you might expect. What I've done is ended up using abbreviated 'namespaces' to keep things short.

import RSV = module("views/researchSectionView")
var r = new RSV.ResearchSectionView();
like image 30
ryan Avatar answered Oct 05 '22 23:10

ryan