#Type Checker and auto complete problem

1 messages · Page 1 of 1 (latest)

wispy island
#
--!strict
-- @zyos73
-- 08/01/25

export type PublicChannel = {
    Type: "Public",
    Name: string,
    AutoJoin: boolean,
    Register: () -> any
}

export type PrivateChannel = {
    Type: "Private",
    Name: string,
    AllowedPlayers: {Player},
    Register: () -> any
}

export type Instances =
    PublicChannel |
    PrivateChannel

export type InstanceSelection =
    "PublicChannel" |
    "PrivateChannel"
    
export type MappedInstances = {
    PublicChannel : PublicChannel,
    PrivateChannel : PrivateChannel
}

return nil

New Function

--!strict
-- @zyos73
-- 08/01/25

-- Modules
local Package = script.Parent.Parent
local Types = require(Package.Types)

-- Types
type InstanceSelection = Types.InstanceSelection
type Instances = Types.Instances
type PublicChannel = Types.PublicChannel
type PrivateChannel = Types.PrivateChannel

-- Instances
local Instance = Package.Instances

local instanceMap = {
    PublicChannel = require(Package.Instances.PuplicChannel),
    PrivateChannel = require(Package.Instances.PrivateChannel)
}

local function New(instanceType : InstanceSelection) : Instance
    local instance = instanceMap[instanceType]()
    return instance
end

return New

Usage (No auto complete for newPrivateChannel.AllowedPlayers & newPublicChannel.AutoJoin)

--!strict
-- @zyos73
-- 08/01/25

-- Modules
local TextChatServiceV2 = require(game.ServerScriptService.TextChatServiceV2)
local Types = require(game.ServerScriptService.TextChatServiceV2.Types)

local player = game.Players:WaitForChild("Zyozok")

local newPrivateChannel = TextChatServiceV2.New("PrivateChannel")
newPrivateChannel.AllowedPlayers = {player}
newPrivateChannel.Register()

local newPuplicChannel = TextChatServiceV2.New("PublicChannel")
newPuplicChannel.Register()
#

So the problem im having is that the type checker cant tell what type im returning in the new() function I was thinking about forcing the type to be Public- or Private-Channel at the very end, for this to work I need to convert the string im getting into a type tho, is that possible? Also if theres another solution for this while not changing the function and setting the type im the Usage Script tell me!

#

I tried the dynamic type thingy with the new type solver already thats pretty buggy

hot niche
#

puplic

wispy island
hot niche
#

ohh

#

why not make separate functions for public and private channel?

wispy island
#

cause I want to handle everything with ``.New("type")likeInstance.New("type")` does

hot niche
#

i really don't know why you would do that

wispy island
#

cause I can seperate things
-> New --creates new Instances
-> etc. etc.

else it would be
-> NewPublicChannel
-> NewPrivateChannel

#

I dont want this many functions under the main API

solar slate
#

PublicChannel = require(Package.Instances.PuplicChannel)
hehe

solar slate
wispy island
#

no shit

solar slate
#
export type Instances =
    PublicChannel |
    PrivateChannel```
#

use template maybe

wispy island
wispy island
#
local function New<T>(instanceType : T & InstanceSelection) : T & Instances
    return MappedInstances[instanceType]
end

if you mean this I tried this and it works but I would have to define the type everytime i want to create a new channel

solar slate
#

that doesn't look right

#

T & InstanceSelection ?

wispy island
#
local function New<T>(instanceType : InstanceSelection) : T & Instances
    assert(MappedInstances[instanceType], `Type not found`)
    return MappedInstances[instanceType]
end

mb this works

#

I dont rlly understand these templates yet

#
local newPrivateChannel : Types.PrivateChannel = TextChatServiceV2.New("PrivateChannel")
newPrivateChannel.AllowedPlayers = {}
newPrivateChannel.Register()

local newPublicChannel : Types.PublicChannel = TextChatServiceV2.New("PublicChannel")
newPublicChannel.AutoJoin = false
newPublicChannel.Register()

Wehn doing this I need to define the Type beforehand which I also don't really want to use wehn theres a better solution

solar slate
#

the signature would just be New<T>(instanceType:InstanceSelection):T

#

you can generalize but i'm not sure how instance.new does it

wispy island
#

how can it be so hard to create the similair api to Instance.new("type")🥀

solar slate
#

aka make a baseclass for the channel then type refine so you get most of the properties for free

wispy island
#

wdym

#

how would a baseclass look like

solar slate
#

wait no the union should already do that, it's been a while for me

wispy island
#

yeah I get like Type, Name and Register to auto complete just not the 2 diffrent properties

#

Ive been at trying to do this for 4-5h now I don't think ima find a solution for this tbfevilcat

solar slate
#

type refinement would work too

#

but its not quite exactly like instance.new

wispy island
#

mm alr ima read ts

#

thanks

solar slate
#

this looks interesting

#
type ChannelInstances = {
  PublicChannel:PublicChannel;
  PrivateChannel:PrivateChannel;
}
local function New(instanceType: keyof<ChannelInstances>)
  local r:index<ChannelInstances, typeof(instanceType)> = MappedInstances[instanceType]()
  return r
end
local priv=New("PrivateChannel")
local pub=New("PublicChannel")
-- autocompleted types?
``` i can't test this in studio
#
wispy island
#

Types

--!strict
-- @zyos73
-- 08/01/25

export type PublicChannel = {
    Type: "Public",
    Name: string,
    AutoJoin: boolean,
    Register: () -> any
}

export type PrivateChannel = {
    Type: "Private",
    Name: string,
    AllowedPlayers: {Player},
    Register: () -> any
}

export type Instances =
    PublicChannel |
    PrivateChannel

export type InstanceSelection =
    "PublicChannel" |
    "PrivateChannel"
    
export type MappedInstances = {
    PublicChannel : PublicChannel,
    PrivateChannel : PrivateChannel
}

return nil

New Function

--!strict
-- @zyos73
-- 08/01/25

-- Modules
local Package = script.Parent.Parent
local Types = require(Package.Types)
local InstanceFunctions = require(script.InstanceFunctions)

-- Types
type MappendInstances =  Types.MappedInstances
type InstanceSelection = Types.InstanceSelection
type Instances = Types.Instances
type PublicChannel = Types.PublicChannel
type PrivateChannel = Types.PrivateChannel

-- Instances
local Instance = Package.Instances



local function New(instanceType : keyof<MappendInstances>)
    assert(InstanceFunctions[instanceType], `Instance not found`)
    local instance: index<MappendInstances, typeof(instanceType)> = InstanceFunctions[instanceType]
    return instance
end

return New

Usage

--!strict
-- @zyos73
-- 08/01/25

-- Modules
local TextChatServiceV2 = require(game.ServerScriptService.TextChatServiceV2)
local Types = require(game.ServerScriptService.TextChatServiceV2.Types)

local player = game.Players:WaitForChild("Zyozok")

local newPrivateChannel = TextChatServiceV2.New("PrivateChannel")
newPrivateChannel.AllowedPlayers = {}
newPrivateChannel.Register()

local newPublicChannel = TextChatServiceV2.New("PublicChannel")
newPublicChannel.AutoJoin = false
newPublicChannel.Register()
#

Sadly it still doesnt detect .AllowedPlayers and .AutoJoin