---
schema_version: "1"
id: 8a0dbb25f0ba1528b41dc165fad51eeea54ed7d5560848fcbce683ec6c17f45e
title: Selecting Types
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.9/dataweave-selecting-types.md
canonical_url: https://docs.mulesoft.com/dataweave/2.9/dataweave-selecting-types
source_site: docs.mulesoft.com
content_type: documentation
content_hash: e0d65b536bb36c8cf75f3b4a14176da4919002d7090f1c34c40bc438433cf344
description: MuleSoft Documentation Site
release: "2.9"
latest: false
---
---
product: DataWeave
version: 2.9
---

# Selecting Types

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

Using the dot selector (`.`) over DataWeave [types](https://docs.mulesoft.com/dataweave/2.9/dataweave-type-system#composite-types) enables
you to declare new types from existing ones. Combined with the
[module loader](https://docs.mulesoft.com/dataweave/2.9/dataweave-type-system#reuse_types), 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:

```dataweave
%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:

```dataweave
%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:

```json
{
  "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:

```dataweave
%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:

```json
[
  {
    "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:

```dataweave
%dw 2.0
output application/json
type UnionType = {name: String} | Number
type Fail = UnionType.name
---
{}
```

Output:

```json
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:

```dataweave
%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:

```json
{
  "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:

```dataweave
%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:

```json
{
  "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:

```dataweave
%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:

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