---
schema_version: "1"
id: 08c7340b16355ac8c885e67f399eca8e08cbc0caff7a20f0b4bbea8728b9cc82
title: 型の選択
collection: mulesoft
version: current
locale: ja-jp
portal_url: https://docs.salesforce.com/mulesoft/current/ja-jp/dataweave/2.6/dataweave-selecting-types.md
canonical_url: https://docs.mulesoft.com/jp/dataweave/2.6/dataweave-selecting-types
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 518f1d1be5016c18291ac5ba784585278a377186dc93cfbb2a3eb016396de903
description: MuleSoft Documentation Site
release: "2.6"
latest: false
---
# 型の選択

DataWeave ​[型](https://docs.mulesoft.com/jp/dataweave/2.6/dataweave-type-system#composite-types)​でドットセレクター (​`.`​) を使用すると、既存の型から新しい型を宣言できます。​[モジュールローダー](https://docs.mulesoft.com/jp/dataweave/2.6/dataweave-type-system#reuse_types)​と組み合わせることで、DataWeave はカスタムモジュールファイルから宣言を読み込み、他の DataWeave モジュールの型と同じようにアクセスできる DataWeave 型ディレクティブに変換することもできます。

## [](#既存の型から新しい型を宣言する)既存の型から新しい型を宣言する

次の例では、​`User`​ 型から ​`NameType`​ を宣言します。​`User.name`​ の形式でドットセレクターを使用して ​`name`​ 変数を ​`NameType`​ として宣言することで、​`typeOf()`​ 関数でプリミティブ型 ​`"String"`​ を返すことができます。DataWeave 変数​`name`​ は、​`NameType`​ 値 "Seba" として宣言されます。

DataWeave スクリプト:

```dataweave
%dw 2.0
type User = {name: String}
type NameType = User.name
var name: NameType = "Seba"
---
typeOf(name)
```

出力:

"String"

## [](#既存の型の項目から型を作成する)既存の型の項目から型を作成する

次の例では、ドットセレクター (​`.`​) を使用して、複雑な型 ​`User`​ の 1 つのキーから新しい型 ​`Address`​ を宣言します。

DataWeave スクリプト:

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

出力:

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

## [](#結合型から型を選択する)結合型から型を選択する

次の例は、​`Object`​ 型で構成される ​`Union`​ 型を使用して型を選択する方法を示しています。

DataWeave スクリプト:

```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)]
```

出力:

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

次のスクリプトは、2 番目の型が ​`Object`​ 型ではなく、セレクターで参照する方法がないため、失敗します。

DataWeave スクリプト:

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

出力:

```json
Cannot do selection on Type: Number

4| type Fail = UnionType.name
```

## [](#名前空間から型を選択する)名前空間から型を選択する

次の例は、名前空間の型の選択を示しています。

DataWeave スクリプト:

```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)
}
```

出力:

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

## [](#型パラメーターを使用して型を選択する)型パラメーターを使用して型を選択する

次の例は、他のプログラミング言語の汎用型に似ている型パラメーターでどのように型が選択されるのかを示しています。

DataWeave スクリプト:

```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,
}
```

出力:

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

## [](#型のメタデータを保持する)型のメタデータを保持する

次の例は、型の選択で型に関連付けられたメタデータが保持されることを示しています。

DataWeave スクリプト:

```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
}
```

出力:

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