Selecting Types | MuleSoft Documentation
# Selecting Types
Using the dot selector (`.`) over DataWeave types enables you to declare new types from existing ones. Combined with the module loader, DataWeave can also load and translate declarations from a custom module file into DataWeave type directives that can be accessed in the same way as types from any other DataWeave module.
## Declare a New Type from an Existing Type
The following example declares the `NameType` from the `User` type. Using the dot selector in `User.name` to declare the `name` variable as `NameType` enables the `typeOf()` function to return the primitive type, `"String"`. The DataWeave variable `name` is declared as a `NameType` value "Seba":
DataWeave script:
```
%dw 2.0
type User = {name: String}
type NameType = User.name
var name: NameType = "Seba"
---
typeOf(name)
```

Output:
```
"String"
```

## Compose a Type from an Existing Type’s Field
The following example uses the dot selector (`.`) to declare a new type `Address` from one key in the complex type `User`:
DataWeave script:
```
%dw 2.0
output application/json
type User = {
 address : {country: String, city: String, street: String, number: Number},
 userName : String
}
type Address = User.address
var userAddress: Address = {country: "Argentina", city: "Rosario", street: "Calle Falsa", number: 123}
---
userAddress
```

Output:
```
{
 "country": "Argentina",
 "city": "Rosario",
 "street": "Calle Falsa",
 "number": 123
}
```

## Select Types from Union Types
The following example shows how you can use type selection with `Union` types that consist of `Object` types:
DataWeave script:
```
%dw 2.0
output application/json
type ID = {id: String, firstName: String, age: Number} | {id: Number, firstName: String, secondName: String}
var nameAge: ID = {firstName: "Seba", id: "123", age: 28}
var fullName: ID = {id: 38123, firstName: "Seba", secondName: "Elizalde"}
var nameString: ID.firstName = "Seba"
var idNumber: ID.id = 38123
---
[nameAge, fullName, nameString ++ " is of type " ++ typeOf(nameString), idNumber ++ " is of type " ++ typeOf(idNumber)]
```

Output:
```
[
 {
  "firstName": "Seba",
  "id": "123",
  "age": 28
 },
 {
  "id": 38123,
  "firstName": "Seba",
  "secondName": "Elizalde"
 },
 "Seba is of type String",
 "38123 is of type Number"
]
```

The following script fails because the second type is not an `Object` type and there is no way to reference it with the selector:
DataWeave script:
```
%dw 2.0
output application/json
type UnionType = {name: String} | Number
type Fail = UnionType.name
---
{}
```

Output:
```
Cannot do selection on Type: Number
4| type Fail = UnionType.name
```

## Select Types from Namespaces
The following example shows type selection of a namespace:
DataWeave script:
```
%dw 2.0
output application/json
ns ns1 http://acme.com
type User = {
 ns1#name : String,
 name : Number,
}
var nameString: User.ns1#name = "Seba"
var nameNumber: User.name = 123
---
{
  "NameString" : typeOf(nameString),
  "NameNumber" : typeOf(nameNumber)
}
```

Output:
```
{
 "NameString": "String",
 "NameNumber": "Number"
}
```

## Select Types with Type Parameters
This example shows how type selection works with type parameters, which are similar to generics in other programming languages:
DataWeave script:
```
%dw 2.0
output application/json
type WithParameters<A, B> = {first: A, second: B, nestedObject: {message: A}}
---
{
 a: true is WithParameters<Boolean, Number>.first,
 b: 4592 is WithParameters<String, Number>.second,
 c: "sdf" is WithParameters<String, Number>.nestedObject.message,
}
```

Output:
```
{
 "a": true,
 "b": true,
 "c": true
}
```

## Preserve Metadata of a Type
This example shows that type selection preserves metadata associated with the type:
DataWeave script:
```
%dw 2.0
output application/json
type User = {
 birthDate: Date {format: "dd-MMM-yy"},
 userName : String {schema: "value"}
}
type FormattedDate = User.birthDate
type UserName = User.userName
var formattedDate: FormattedDate = "10-SEP-15" as Date {format: "dd-MMM-yy"}
var otherFormatDate = "23-10-2022" as Date {format: "dd-MM-yyyy"}
var userName = "Messi" as String {schema: "value"}
var otherUserName = "Di María" as String {schema: "otherValue"}
---
{
formattedDate: formattedDate is FormattedDate,
userName: userName is UserName,
otherFormatDate: otherFormatDate is FormattedDate,
otherUserName: otherUserName is UserName
}
```

Output:
```
{
 "formattedDate": true,
 "userName": true,
 "otherFormatDate": false,
 "otherUserName": false
}
```