2

I am moving from Vue2 to Vue3 but couldn't manage to transfer some objects to new ref method.

How do I write this with ref with types? I did some research but couldn't find much.

 data() {
    return {
      isDeleteEnabled: true,
      contractSet: {
        contract: {
          id: null,
          name:''
          is_deleted: null,
          created_datetime: '',
        },
        services: [
          {
            id: null,
            name: '',
            is_disabled: null,
            is_deleted: null,
          },
        ],
      } as ContractSetsType,
    };
  },  

Started like this but it gives type errors: (No overload matches this call)

    let isDeleteEnabled= ref<boolean>(false); // This works fine

    // This gives type error
    let contractSet = ref<ContractSetsType>({
    contract: {
              id: null,
            },
    });

My ContractSetsType:

 export interface ContractSetsType {
        /**
         * 
         * @type {Contract}
         * @memberof ContractSetsType
         */
        contract?: Contract;
    }
    
    export interface Contract {
        /**
         * 契約id
         * @type {number}
         * @memberof Contract
         */
        id: number;
    
        /**
         * 契約名
         * @type {string}
         * @memberof Contract
         */
        name: string;
    
        /**
         * 削除済フラグ
         * @type {boolean}
         * @memberof Contract
         */
        is_deleted: boolean;
    
        /**
         * 作成日時
         * @type {string}
         * @memberof Contract
         */
        created_datetime: string;
    }

Should I use reactive instead of ref?

2
  • How is your ContractSetsType defined? Commented Apr 25, 2022 at 5:07
  • @cSharp added above. Commented Apr 25, 2022 at 5:34

1 Answer 1

4

Your ContractSetsType is different from below.

{
    contract: {
        id: null    
    }
}

You have to match the type:

//below works fine
let contractSet = ref<ContractSetsType>({
    contract: {
        id: 12,
        name: "my name",
        is_deleted: false,
        created_datetime: "timestamp string"
    }
})

let contractSet0 = ref<ContractSetsType>({
})

// below doesn't work
let contractSet1 = ref<ContractSetsType>({
    contract: {
        id: 12,
        name: "my name",
    }
})

let contractSet2 = ref<ContractSetsType>({
    contract: {
        id: 12,
    }
})

let contractSet3 = ref<ContractSetsType>({
    contract: {
        id: null
    }
})

If you want

let contractSet = ref<ContractSetsType>({
    contract: {
        id: null
    }
})

to work, you could change interface Contract to have everything optional.

Sign up to request clarification or add additional context in comments.

2 Comments

I added the wrong types. Sorry. Could you take a look at the code again?
Still, you have to match your correct types. Edited answer to give examples.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.