Skip to content

Commit

Permalink
fix: issue with functions in useStore for typescript (#1576)
Browse files Browse the repository at this point in the history
  • Loading branch information
nmerget authored Oct 1, 2024
1 parent 7e2c95f commit d59d328
Show file tree
Hide file tree
Showing 13 changed files with 29 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/lemon-melons-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@builder.io/mitosis': patch
---

[Angular, React, Vue] fix: issue with functions inside `useStore` missing ReturnType<...> when using `typescript: true` in config
Original file line number Diff line number Diff line change
Expand Up @@ -13957,14 +13957,14 @@ type GetterStore = {
})
export default class TypeGetterStore {
name: GetterStore[\\"name\\"] = \\"test\\";
getName(): GetterStore[\\"getName\\"] {
getName(): ReturnType<GetterStore[\\"getName\\"]> {
if (this.name === \\"a\\") {
return \\"b\\";
}

return this.name;
}
get test(): GetterStore[\\"test\\"] {
get test(): ReturnType<GetterStore[\\"test\\"]> {
return \\"test\\";
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14181,14 +14181,14 @@ type GetterStore = {
})
export default class TypeGetterStore {
name: GetterStore[\\"name\\"] = \\"test\\";
getName(): GetterStore[\\"getName\\"] {
getName(): ReturnType<GetterStore[\\"getName\\"]> {
if (this.name === \\"a\\") {
return \\"b\\";
}

return this.name;
}
get test(): GetterStore[\\"test\\"] {
get test(): ReturnType<GetterStore[\\"test\\"]> {
return \\"test\\";
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14388,14 +14388,14 @@ type GetterStore = {
})
export default class TypeGetterStore {
name: GetterStore[\\"name\\"] = \\"test\\";
getName(): GetterStore[\\"getName\\"] {
getName(): ReturnType<GetterStore[\\"getName\\"]> {
if (this.name === \\"a\\") {
return \\"b\\";
}

return this.name;
}
get test(): GetterStore[\\"test\\"] {
get test(): ReturnType<GetterStore[\\"test\\"]> {
return \\"test\\";
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12530,14 +12530,14 @@ type GetterStore = {
})
export default class TypeGetterStore {
name: GetterStore[\\"name\\"] = \\"test\\";
getName(): GetterStore[\\"getName\\"] {
getName(): ReturnType<GetterStore[\\"getName\\"]> {
if (this.name === \\"a\\") {
return \\"b\\";
}

return this.name;
}
get test(): GetterStore[\\"test\\"] {
get test(): ReturnType<GetterStore[\\"test\\"]> {
return \\"test\\";
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26271,14 +26271,14 @@ type GetterStore = {
})
export default class TypeGetterStore {
name: GetterStore[\\"name\\"] = \\"test\\";
getName(): GetterStore[\\"getName\\"] {
getName(): ReturnType<GetterStore[\\"getName\\"]> {
if (this.name === \\"a\\") {
return \\"b\\";
}

return this.name;
}
get test(): GetterStore[\\"test\\"] {
get test(): ReturnType<GetterStore[\\"test\\"]> {
return \\"test\\";
}
}
Expand Down Expand Up @@ -26319,14 +26319,14 @@ type GetterStore = {
})
export default class TypeGetterStore {
name: GetterStore[\\"name\\"] = \\"test\\";
getName(): GetterStore[\\"getName\\"] {
getName(): ReturnType<GetterStore[\\"getName\\"]> {
if (this.name === \\"a\\") {
return \\"b\\";
}

return this.name;
}
get test(): GetterStore[\\"test\\"] {
get test(): ReturnType<GetterStore[\\"test\\"]> {
return \\"test\\";
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/__tests__/__snapshots__/preact.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7153,15 +7153,15 @@ type GetterStore = {
function TypeGetterStore(props: any) {
const [name, setName] = useState<GetterStore[\\"name\\"]>(() => \\"test\\");

function getName(): GetterStore[\\"getName\\"] {
function getName(): ReturnType<GetterStore[\\"getName\\"]> {
if (name === \\"a\\") {
return \\"b\\";
}

return name;
}

function test(): GetterStore[\\"test\\"] {
function test(): ReturnType<GetterStore[\\"test\\"]> {
return \\"test\\";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9806,15 +9806,15 @@ type GetterStore = {
function TypeGetterStore(props: any) {
const [name, setName] = useState<GetterStore[\\"name\\"]>(() => \\"test\\");

function getName(): GetterStore[\\"getName\\"] {
function getName(): ReturnType<GetterStore[\\"getName\\"]> {
if (name === \\"a\\") {
return \\"b\\";
}

return name;
}

function test(): GetterStore[\\"test\\"] {
function test(): ReturnType<GetterStore[\\"test\\"]> {
return \\"test\\";
}

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/__tests__/__snapshots__/react.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6962,15 +6962,15 @@ type GetterStore = {
function TypeGetterStore(props: any) {
const [name, setName] = useState<GetterStore[\\"name\\"]>(() => \\"test\\");

function getName(): GetterStore[\\"getName\\"] {
function getName(): ReturnType<GetterStore[\\"getName\\"]> {
if (name === \\"a\\") {
return \\"b\\";
}

return name;
}

function test(): GetterStore[\\"test\\"] {
function test(): ReturnType<GetterStore[\\"test\\"]> {
return \\"test\\";
}

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/__tests__/__snapshots__/rsc.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6436,15 +6436,15 @@ type GetterStore = {
function TypeGetterStore(props: any) {
const [name, setName] = useState<GetterStore[\\"name\\"]>(() => \\"test\\");

function getName(): GetterStore[\\"getName\\"] {
function getName(): ReturnType<GetterStore[\\"getName\\"]> {
if (name === \\"a\\") {
return \\"b\\";
}

return name;
}

function test(): GetterStore[\\"test\\"] {
function test(): ReturnType<GetterStore[\\"test\\"]> {
return \\"test\\";
}

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/__tests__/__snapshots__/taro.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7114,15 +7114,15 @@ import { View, Text } from \\"@tarojs/components\\";
function TypeGetterStore(props: any) {
const [name, setName] = useState<GetterStore[\\"name\\"]>(() => \\"test\\");

function getName(): GetterStore[\\"getName\\"] {
function getName(): ReturnType<GetterStore[\\"getName\\"]> {
if (name === \\"a\\") {
return \\"b\\";
}

return name;
}

function test(): GetterStore[\\"test\\"] {
function test(): ReturnType<GetterStore[\\"test\\"]> {
return \\"test\\";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6359,7 +6359,7 @@ const test = computed(() => {
return \\"test\\";
});

function getName(): GetterStore[\\"getName\\"] {
function getName(): ReturnType<GetterStore[\\"getName\\"]> {
if (name.value === \\"a\\") {
return \\"b\\";
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/helpers/get-typed-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ export const getTypedFunction = (code: string, typescript?: boolean, typeParamet
const preType = code.slice(0, firstParenthesisIndex - 1);
const postType = code.slice(firstParenthesisIndex, code.length);

return [preType, ': ', typeParameter, postType].join('');
return [preType, ': ', `ReturnType<${typeParameter}>`, postType].join('');
};

0 comments on commit d59d328

Please sign in to comment.